TOTSCR - Editorial

Testcase given in problems passed but code did not get submitted.
can someone figure out the mistake?

import java.io.IOException;
import java.util.Scanner;

class codeche
{
public static void main(String[] args) throws IOException
{
try
{
Scanner sc = new Scanner(System.in);
byte t,i=0;
t = sc.nextByte();
while(i< t)
{
short n,k;
n = sc.nextShort();
k = sc.nextShort();
int arr[] = new int[k];
for(int j=0;j<k;j++)
{
arr[j] = sc.nextInt();
}
String s1 = sc.next();
String s2 = sc.next();

			byte res =0,res1=0;
			for(int j=0;j<k;j++)
			{
				res+= ((int)s1.charAt(j)-48)*arr[j];
				res1+= ((int)s2.charAt(j)-48)*arr[j];
			}
			System.out.println(res);
			System.out.println(res1);
			i++;
				
		}
		sc.close();
	}
	catch (Exception e) 
	{
		return;
	}
}

}

total needs to be long int!

Getting runtime error but given testcases were passed

Need help in finding out the reason why code gives runtime error

#include <bits/stdc++.h>
#define in(n, arr)              \
    for (int i = 0; i < n; i++) \
    cin >> arr[i]
#define out(n, arr)             \
    for (int i = 0; i < n; i++) \
    cout << arr[i]
#define lli long long int
#define pb push_back
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);


#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    int t;
    cin >> t;
    while (t--)
    {
        long long int n, k;
        cin >> n >> k;
        long long int peopleScore[n];
        for (int i = 0; i < k; i++)
            cin >> peopleScore[i];

        while (n--)
        {
            char s[k];
            for (int i = 0; i < k; i++)
            {
                cin >> s[i];
            }
            int total = 0;
            for (int i = 0; i < k; i++)
                if (s[i] == '1')
                    total += peopleScore[i];

            cout << total << "\n";
        }
    }

    return 0;
}

What if n < k?
Of course, it seems they didn’t use standard symbols to denote entities. And for god sake, why don’t they include these type of inputs in sample input?

1 Like

This code paased sample test cases but don’t understood why not accepted by Online Judge…
Can you show me my mistakes

#include

#include

#include

using namespace std;

int main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin>>t;

while(t--)

{

    int n,k,res=0,len=0;

    cin>>n>>k;

    int arr[k];

    string s="",d1="",d2="";

    for(int i=0;i<k;i++)

    {

        int x;

        cin>>x;

        arr[i] = x;

    }

    for(int i=0;i<n;i++)

    {

        res=0;

        cin>>s;

                                        

                if(s[0] == '1')

                {

                    res = res+arr[0];

                }

                if(s[1] == '1')

                {

                    res = res+arr[1];

                }

            

        cout<<res<<"\n";

    }

                

}

return 0;   

}

Can someone please explain why am I getting wrong answer for my code on submission. It passed all the testcases and I checked with many more testcases.

#include <iostream>
#include<string>

using namespace std;

int main() {
	// your code goes here
	int T;
	cin>>T;
	while(T--){
	    int n,k;
	    cin>>n>>k;
	    int A[k];
	    string S[n];
	    for(int i = 0; i< k; i++){
	        cin>>A[i];
	    }
	    for(int i = 0; i<n;i++ ){
	    	cin>>S[i];
	    }
	    for(int i = 0; i < n; i++){
	        int sum = 0;
	        for(int j =0;j<k;j++){
	            if(S[i][j] == '1'){
	                sum += A[j];
	            }
	        }
	        cout<<sum<<endl;
	    }
	}
	return 0;
}```

@ranjeethinge and @secret_santa , paste your code in between a pair of 3 back ticks (```).
Or provide a link to your submission. This is a simple problem. You might want to figure it out yourself.

I got it, thanks for pointing it out

I made the required changes. Please help me sort out why am I getting wrong answer. The problem was very simple and I used the same logic which was given at the start of the post.

#include<string>

using namespace std;

int main() {
	// your code goes here
	int T;
	cin>>T;
	while(T--){
	    int n,k;
	    cin>>n>>k;
	    int A[k];
	    string S[n];
	    for(int i = 0; i< k; i++){
	        cin>>A[i];
	    }
	    for(int i = 0; i<n;i++ ){
	    	cin>>S[i];
	    }
	    for(int i = 0; i < n; i++){
	        int sum = 0;
	        for(int j =0;j<k;j++){
	            if(S[i][j] == '1'){
	                sum += A[j];
	            }
	        }
	        cout<<sum<<endl;
	    }
	}
	return 0;
}
1 Like

I think it should be long long , as I made the same error & after changing it got accepted

1 Like

Yeah, as said by @mittal_sagar , the answer would not fit in an Integer. You should use bigger one, say long or long long. I guess there are no other mistakes.

thank you!

got it thank you!!

thanks bro it worked.
but i have a doubt, In c++ long int and int both are 4 bytes so why int is not working?

I used long long, long long int, and even int. But none of them worked. Gave me WA.
Can anyone tell me what’s wrong with my code?

My code:

 #include <iostream>
using namespace std;

 int main()
{

    long long t;

    long long n, k;

    cin >> t;

    long long scr = 0;
    while (t--)
    {
        cin >> n >> k;

        long long a[k];

        for (size_t i = 0; i < k; i++)
        {
            cin >> a[i];
        }

        string s;

        for (size_t i = 0; i < n; i++)
        {
            scr = 0;
            cin >> s;

            for (size_t j = 0; j < 2; j++)
            {

                if (s[j] - '0' == 1)
                {
                    scr += a[j];
                }

                else if (s[j] - '0' == 0)
                    scr += 0;
            }
            cout << scr << endl;
        }
    }
}

my code passed the given sample test case but is showing the wrong answer on submission. can ayone help me?

:::::::::::code::::::::::::

#include
#include<bits/stdc++.h>

using namespace std;

int score(string str,int arr[],int k)
{
int sum=0;
for(int i=0;i<str.length();i++)
{
if(str[i]==‘0’)sum=sum+0;
else if(str[i]==‘1’)
{
sum=sum+arr[i];
}
}
return sum;
}

int main()
{
int t;
cin >> t;

while (t>0)
{
	int n,k;
    cin >> n>>k;
    int a[k];

    for(int i = 0; i < k; ++i)
    {
        cin >> a[i];
    }

    string s[n];
    for(int i = 0; i < n; ++i)
    {
        cin >> s[i];
    }

    for(int i=0;i<n;i++)
        cout << score(s[i],a,k) << endl;
    t--;
}

return 0;

}

Hey my code is giving tle error can anybody plss help how can i optimize it ??
code is here - CodeChef: Practical coding for everyone

Your code will work for test cases
As test cases had situation of 2 questions
What if there are 3 questions and array had 3 elements
I guess your code wont work in that case
I suggest read the problem again

I tried with case like there are 3 questions and 2 people solving it in that case it also worked

Hey ,Thanks for replying
I used total as long long instead of int and it got submitted.