Kindly Help with the code error -

I am getting Wrong Answer although it is compiling and giving the right answer.
Problem Link
https://www.codechef.com/problems/DANOW
My code
#include
using namespace std;

int main(){
int n , q;
int i , j;
int l , r;
int x , y;
int sum = 0;
cin >> n >> q;
int a[n],b[n];
for(i = 1;i <= n;i++){
cin >> a[i];
}
for(j = 1;j <= n;j++){
cin >> b[j];
}

while(q--){
    cin >> l >> r ;
    sum = 0;
    for(x = l; x <= r; x++){

        sum = sum + (a[x]*b[x]);

    }
    cout << sum << endl;
}

}
And the submitted answer link
https://www.codechef.com/viewsolution/25029277

In the top Header file in included but got removed in that post .
Thanks for the help .

change datatype of sum and arrays to long long.

2 Likes

Since the max value of A[i]= 10^6 and max value of B[i] =10^6.
So on multiplcation, it will exceed your int range (i.e 10^{12}). On iteration to 1000, the sum value will be around 10^{15}. So make int to long long int. :slightly_smiling_face:

1 Like

Worked Thank You so much

Thank you so much for your help