This is my solution to Spoj 11373. Coke madness
#include <cstdio>
int main(){
int T; scanf("%d",&T);
for(int j = 1; j <= T; j++){
int n;
scanf("%d",&n);
long long sum = 0, small = 0;
int t;
for(int i = 0; i < n; i++) {
scanf("%d",&t);
sum += t;
if( sum < small) small = sum;
}
printf("Scenario #%d: %lld\n", j, -1*small+1);
}
}
The problem is simple. This solution takes 0.12 seconds on Spoj, though there are 0.01 second solutions. I am curious what optimizations may be done to this code to get faster. I thought -1*small+1 may be got using bit-wise operations but didn't find how. Also I can't get rid of long long since sum may exceed int.
-1*small+1to1 - smallintoutside of a loop and one inside of it?