WA in KINGSHIP

i am continuously getting WA in [kingship][1]

what’s the reason??
here is the code

#include <cstdio>
using namespace std;

int main() {
int t;
scanf("%d", &t);

while (t--) {
	long long int n, arr[100005], i, min;
	scanf("%ld", &n);
	
	long long int res = 0, cnt = 0;
	if (n == 1) {
		res = 0;
	} else  {
		scanf("%ld", &arr[0]);
		min = arr[0];
		for (i = 1;i < n;i++) {
			scanf("%ld", &arr[i]);
			if (min > arr[i]) {
				min = arr[i];
			}
		}
		for (i = 0;i < n;i++) {
		if (arr[i] == min && cnt == 0) {
			cnt++;
			continue;
		}
		res += (arr[i] * min);
	}
	}
	
	printf("%lld\n", res);
}
return 0;

}
[1]: http://www.codechef.com/problems/KINGSHIP

Try unsigned long long instead of long long.

1 Like

For input

1
3
1000000 1000000 1000000

it returns -1454759936

1 Like

its not returning the above…here is the link http://ideone.com/9cb0iH

Please, common on!

You have to try to submit it if you want to know if it works…

Your last 3 submissions:

Your last submission

  • http://www.codechef.com/viewsolution/3438107 -> long int n, arr[100005]

?!?

sorry but i am not able to understand what u r trying to say

Simply change it on both places - for arr and res to long long

ok… i got the correct answer but i am still confused…when it was mentioned in the question that range of array is 1 ≤ Pi ≤ 10^6…then what is the need of taking long long for array??

because of arr[i] * min in your code, if both are ints there is overflow…

2 Likes

another question is when i am taking unsigned long long int i am getting SIGSEGV…what is the reason for this??

maybe because of memory - move arr out of main…