WWALK - Editorial

Getting WA in second subtask even after using long long. Can anyone explain?

#include <bits/stdc++.h>
using namespace std;

#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector vi;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t;
cin>>t;
while(t–)
{
ll n;
cin>>n;
int a[n+5],b[n+5];
f(i,n) cin>>a[i];
f(i,n) cin>>b[i];
int posa=0,posb=0,dis=0;
f(i,n)
{
if(a[i]==b[i]&&posa==posb) dis+=a[i];
posa+=a[i];posb+=b[i];
}
cout<<dis<<endl;
}

}

Use long long int for posa,posb and dis variables.

this is my solution what is wrong in it ??
#include<bits/stdc++.h>
using namespace std;
long long int t,i,n,s=0,b,c=0,d=0;
main()
{
cin>>t;
while(t–)
{
cin>>n;
int a[n],b[n];
for(i=0;i<n;i++)cin>>a[i],s=s+a[i];
for(i=0;i<n;i++)cin>>b[i],c=c+b[i];
for(i=0;i<n-1;i++)
{
if((s==c)&&(a[i+1]==b[i+1]))
d=d+a[i+1];
}
if(a[0]==b[0])
d=d+a[0];
cout<<d<<endl;
s=0;
d=0;
c=0;
}
}

//what is wrong with my code
#include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
int T;
cin>>T;
while(T–){
long long N;
cin>>N;
long long A[N], B[N];
for(int i=0;i<N;i++){
cin>>A[i];
}
for(int i=0;i<N;i++){
cin>>B[i];
}
long long sumA=0, sumB=0, total=0;
for(int i=0;i<N;i++){

        if(sumB==sumA && A[i]==B[i]){
            total+=A[i];
        }
        sumA+=A[i];
        sumB+=B[i];
    }
    cout<<total;
   
    
}
return 0;

}

Your code is absolutely fine, you just have to print answer for each testcase in a new line

oh yes :sweat_smile:,Thanks

You are missing to check s==c in if(a[0]==b[0]).

// Created by Manmeet Singh Parmar
// name of snippet-> temp.sublime-snippet
// path -> sublime text 3/packages/user/temp.sublime-snippet

#include <bits/stdc++.h>
using namespace std;
#define int long long

typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;

int32_t main()
{
   	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin>>t;
    while(t--){
    	int n; cin >> n;
    	vi A(n);
    	FOR(i,n) cin >> A[i];
    	vi B(n);
    	FOR(i,n) cin >> B[i];
    	int c = 0, A_s = 0, B_s = 0;
    	for(int i=0; i<n; ++i) {
    		A_s += A[i];
    		B_s += B[i];
    		if(A_s == B_s && A[i]==B[i])
    			c += B[i];
    	}
    	cout << c << '\n';
    }
    return 0;
}

/* package codechef; // don’t place package name! */

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

/* Name of the class has to be “Main” only if the class is public. */
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();
int M=1000001;
while(t–!=0){
int n=sc.nextInt();
int arr[]=new int[n];
int brr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
for(int i=0;i<n;i++)
brr[i]=sc.nextInt();
long sum1[]=new long[n+1];
long sum2[]=new long[n+1];
long res=0;
int j=0;
for(int i=0;i<n;i++){
sum1[i+1]=sum1[i]+arr[i];
sum2[i+1]=sum2[i]+brr[i];
}
for(int i=1;i<=n;i++){
// System.out.print(sum1[i]+" "+sum2[i]+“end”);
if(sum1[i-1]==sum2[i-1]&&sum1[i]==sum2[i])
res+=(sum1[i]-sum1[i-1]);
}
System.out.println(res);
}
}
}

thank you sir for helping me.
i am success fully done this program.

import java.util.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main(String[] args)
{
Scanner num = new Scanner(System.in);
int t = num.nextInt();

    while(t!=0){
    int n = num.nextInt();   
    int[] a=new int[n];
    int[] b=new int[n];
    int s1=0,s2=0; 
    int time=0;
    for(int i=0;i<n;i++)
    {
        a[i]=num.nextInt();
        
    }
    for(int i=0;i<n;i++)
    {
        b[i]=num.nextInt();
        
    }
    for(int i=0;i<n;i++)
    {
      s1+=a[i];
      s2+=b[i];
      if( s1==s2 && a[i]==b[i])
        time+=a[i];
    }
    System.out.println(time);
   
    
    t--;
    }
    
          
}

}

i am getting subtask 2 as wa, but subtask 1 runs fine. can someone point out the error?

Your code is absolutely fine, just use long instead of int.

1 Like

solution
t = int(input())

for i in range(t):
summ = 0
position_alice = 0 #at x= 0 position of alice and bob
position_bob = 0
n = int(input())
alice = list(map(int,input().split()))
bob = list(map(int,input().split()))
for i in range(len(alice)):
if alice[i] == bob[i] and position_alice == position_bob:
summ += alice[i]
position_alice += alice[i]
position_bob += bob[i]
#print(position_alice,position_bob)
print(summ)

for _ in range(int(input())):
N = int(input())
da = 0
db = 0
wd = 0
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(N):
if(da == db):
if(a[i] == b[i]):
wd += a[i]
da += a[i]
db += b[i]
# print(da, db)
print(wd)

solution link: CodeChef: Practical coding for everyone
problem link: CodeChef: Practical coding for everyone (WWALK)

Contest : May Lunchtime 2020 Div 2
link: Contest Page | CodeChef

1 Like

thanks!

plz help me where i was wrong.

#include
using namespace std;

int main() {
long long int t,s=0;cin>>t;
for(int i=0;i<t;i++)
{
long long int n;cin>>n;
int a[1000000],b[1000000];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cin>>b[i];
}
for(int j=0;j<n;j++)
{
if(a[j]==b[j])
{
s=s+a[j];
}
}
cout<<s<<endl;
s=0;

    }

}

First you should format your code or give link to your submission. Your mistake is that you are only checking if their speed is equal or not. There should be one more condition that they are at same position.
You may see this CodeChef: Practical coding for everyone

plz will u explain me your code

In array suma[] and sumb[] I have simply calculated the distance they have covered till ith second. So, now if for some time they have same value (suma[i] and sumb[i]) then this means that are at same position at this time. Now, if their speed is also equal then they will cover that distance together. Hope you understood.

3 Likes

Hi Sebastian, you can check my code for reference, I have applied a similar approach :slightly_smiling_face:

int main()
{
	zapp;
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,i;
	    cin>>n;
	    vector<ll> a(n),b(n);
	    for(i=0;i<n;i++)
	    	cin>>a[i];
	    for(i=0;i<n;i++)
	    	cin>>b[i];
	    ll suma = 0, sumb = 0,tog = 0;
	    for(i=0;i<n;i++)
	    {
	    	if(suma == sumb && a[i] == b[i])
	    		tog+=a[i];
			suma+=a[i];
			sumb+=b[i];
		}
		cout<<tog<<"\n";
	}
}
1 Like