How is my solution incorrect?

contest : CCRC21C
problem : TOTSCR
link to problem : CodeChef: Practical coding for everyone
my solution :

#include<bits/stdc++.h>

using namespace std;

int main()
{ int t;
cin>>t;
while(t>0)
{ int score =0,n,k;
string s =“”;
cin>>n>>k;

    int A[k];
   
    
    for(int i=0;i<k;i++)
    cin>>A[i];
   
    
    for(int j=0;j<n;j++)
 {    cin>>s;
    int score=0;
      for(int k=0;s[k]!='\0';k++)
        if(s[k] =='1')
        score += A[k];
        else
          continue;
     
     
     cout<<score<<'\n';  
 }   
     
    
      
    t--;
}

return 0;

}

can anyone explain me how this solution is incorrect. please.

Maybe because you declared score as an integer. The sum may have become larger than the max limit of int for the particular test case.

I changed score from int to long long and got AC. Submission

#include<bits/stdc++.h>

using namespace std;


int main()
{ int t;
    cin>>t;
    while(t>0)
    {  int score =0,n,k;
       string s ="";
        cin>>n>>k;
        
        int A[k];
       
        
        for(int i=0;i<k;i++)
        cin>>A[i];
       
        
        for(int j=0;j<n;j++)
     {    cin>>s;
        long long score=0;
          for(int k=0;s[k]!='\0';k++)
            if(s[k] =='1')
            score += A[k];
            else
              continue;
         
         
         cout<<score<<'\n';  
     }   
         
        
          
        t--;
    }
    
    return 0;
    
}
1 Like

dude i just changed all int values to long long and it worked .
how stupid of me .
i wasted hours thinking that my logic is wrong .
holy shit i am pissed off XD

1 Like

For next time: Simple Trick to Detect Integer Overflow (C/C++) :slight_smile:

1 Like