-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalien.c
67 lines (64 loc) · 899 Bytes
/
alien.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#include<limits.h>
int main()
{
int t,n,m,a[100100],i,j,count,maxcount,sum,minsum;
int left;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
maxcount=count=0; minsum=INT_MAX; sum=0;
left=0;
for(i=0;i<n;i++)
{
if(a[i]>m)
{
sum=0; count=0; left=i+1;
continue;
}
sum+=a[i];
if(sum<=m)
{
count++;
if(count>maxcount){
maxcount=count;
minsum=sum;
}
else if(count==maxcount)
{
if(minsum>sum)
minsum=sum;
}
}
else
{count++;
while(sum>m&&left<i)
{
sum-=a[left];
left++;
count--;
}
if(sum<=m)
{
if(count>maxcount){
maxcount=count;
minsum=sum;
}
else if(count==maxcount)
{
if(sum<minsum)
minsum=sum;
}
}
}
}
if(minsum!=INT_MAX)
printf("%d %d\n",minsum,maxcount);
else
printf("0 0\n");
}
return 0;
}