TOTSCR - Editorial

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 - Solution: 43042928 | CodeChef

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.

Why j<2?
Shouldn’t it be j<k?

Use long long dude.

can anyone tell me how to solve this without if and ternary operator

Simple. See the following snippet.

char s[20000];
scanf("%s",s);
long long int score = 0;
for(int i=0;i<k;i++)
    score += a[i] * (s[i] - '0');
printf("%lld\n", score);

Run this under a loop of N

1 Like

The constraints of N and K were not mentioned separately. What if K = 1000000 and N = 1. It would still satisfy N * K <= 1000000. But most of the solutions would fail if this input is given.
Edit: I meant, if the size of array is statically declared.

import java.util.;
import java.lang.
;
import java.io.*;

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T–>0){
int N = sc.nextInt();
int K = sc.nextInt();
long arr[] = new long[K];
for (int i=0; i<K; i++){
arr[i] = sc.nextLong();
}
String []s = new String[N];
for (int i=0; i<N; i++) {
s[i] = sc.next();
}
for (int i=0; i<N; i++){
long out=0;
for (int j=0; j<K; j++) {
if(s[i].charAt(j) == ‘1’){
out += arr[j];
}
}
System.out.println(out);
}
}
}
}

Can anyone tell me why its giving me TLE error

int is usually 4 bytes, long int has 4 bytes minimum, but nowadays it is usually 8 bytes.

#include<stdio.h>
#include<stdlib.h>
int main(){
int N, K, T;
long long int score;

scanf("%d", &T);

for(int i=0; i<T; i++){
    scanf("%d %d", &N, &K);

    long long int A[K];
    char S[N][K];

    for(int j=0; j<K; j++){
        scanf("%lld", &A[j]);
    }
    for(int j=0; j<N; j++){
        scanf("%s", S[j]);
    }
    for(int j=0; j<N; j++){
        score = 0;
        for(int l=0; l<K; l++){
            if(S[j][l] == '1'){
                score += A[l];
            }
        }
        printf("%lld\n", score);
    }
}
return 0;

}

my program is working fine for provided test case but when I’m submitting, It is saying wrong output…?
why is this happening…?

Consider, taking “total” as long long instead of long int. If the logic is correct, it should work.

B R U H
Why did I change it damnit.
I used s.length() though at first and then I was just debugging with 2.
Damnit

thanks a lot

1 Like

Hi folks,

I have been trying this problem for a few days, it passes all the test cases but while submitting it shows WA(Wrong Answer). I have tried all possible methods but in vain.

Please have a look at my solution

Thanks