forked from arpitbbhayani/spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALIEN.c
49 lines (41 loc) · 877 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
#include <stdio.h>
#include <stdlib.h>
int main ( int argc , char * argv[] ) {
int t;
scanf("%d" , &t);
while ( t-- ) {
int n,b,i;
scanf("%d%d" , &n , &b);
int * array = (int *) malloc ( n*sizeof(int) );
for ( i = 0 ; i < n ; i++ ) {
scanf("%d" , &array[i]);
}
long long int sum = 0;
long long int max_sum = 0;
int size = 0;
int max_size = 0;
int x = 0;
for ( i = 0 ; i < n ; i++ ) {
if ( sum + array[i] <= b ) {
sum += array[i];
size += 1;
}
else {
while ( sum + array[i] > b ) {
sum -= array[x];
size -= 1;
x++;
}
sum += array[i];
size += 1;
}
if ( (size > max_size) || ( max_size == size && max_sum > sum ) ) {
max_sum = sum;
max_size = size;
}
}
printf("%lld %d\n" , max_sum , max_size);
free(array);
}
return 0;
}