Rafsan's blog

By Rafsan, 11 years ago, In English

Following code shows correct output in my CodeBlocks::10.05 but, in CODEFORCES CUSTOM TEST it shows incorrect output.

Main problem is that, in calculation block the value of sum is initialized to 0 in each iteration.

After long experiment I found that
- if I change line 19: form sum+=(i-1)*arr[i]-prevsum; to sum=sum+(i-1)*arr[i]-prevsum; then it shows correct result.
- Or,if I declare array as int instead of long long then it shows correct result.

But I can not able to figure out "why value of sum is becoming 0 in each iteration??". Somebody please help me..

#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;
LL arr[100005];
int main()
{
    int n;
    cin>>n;
    LL sum=0,prevsum=0;
    for(int i=1;i<=n;i++)
    {
       cin>>arr[i];
    }
//calculation block starts here
    for(int i=1;i<=n;i++)
    {
    cout<<"sum : "<<sum<<endl;
    sum+=(i-1)*arr[i]-prevsum;
    prevsum+=arr[i];
    }
//calculation block ends here
    cout<<"Final Result: "<<sum<<endl;

    return 0;
}
/*
Input : 
5
1 2 3 4 5
*/

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it