BUILDB - Editorial

I think this may probably helpful if you are getting the wrong answer–> BODYBUILDER, WHERE WAS THE MISTAKE? - #13 by sofia_001

@shubh1114 thankyou mate :slight_smile:
I forgot to include the max condition there . sillyyyyyyyyy :upside_down_face:

I think if you add the line number 34 code also after line number 36 i.e. you have to check for the max tension after decreasing the tension also.

I don’t think there is a need since we decrease the tension; it can’t be maximum tension.

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,r;
cin>>n>>r;
vector a;
vector b;
for(int i=0;i<n;i++){
long long int k;
cin>>k;
a.push_back(k);
}

	for(int i=0;i<n;i++){
		long long int k;
		cin>>k;
		b.push_back(k);
	}
	
	long long int sum=0;
	for(int i=0;i<n;i++){
		sum+=b[i];
		if(i<n-1)
		{
			sum=sum-(r*(abs(a[i]-a[i+1])));
		}
}
if(sum<0){
	sum=0;
}
cout<<sum<<endl;
}

}

Can anyone tell me where have I gone wrong?

can you please help me also in solving my doubt @thetufguy

WHY CANT WE FIRST ADD ALL THE TENSION UNITS
ll tension = 0;
for (ll i = 0; i < n; i++)
{
tension = tension + b[i];
}

AND THEN CALCULATE THE REST UNITS
ll rest = 0;
for (ll i = 1; i < n; i++)
{
rest = rest + r * (a[i] - a[i - 1]);
}

AND THEN SUBTRACT THE REST UNITS FROM TENSION UNITS
ll final_tension = 0;
final_tension = tension - rest;
if (final_tension < 0)
cout << 0 << endl;
else
cout << final_tension << endl;

MY SAMPLE TEST CASES ARE PASSING BUT WHILE SUBMITTING ITS GIVING WRONG ANS AND I HAVE TRIED ALL TYPE OF TESTCASES EXCEPT FOR LARGE TEST CASES
CAN YOU PLEASE TELL ME WHERE IS THE MISTAKE IN MY CODE AND WHAT KIND OF TEST CASE MIGHT BE THAT WHICH IS FAILING
MY FULL CODE LINK

CODE_LINK

/* 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);
List output=new ArrayList<>();
if(sc.hasNext())
{
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
long n=sc.nextLong();
long r=sc.nextLong();
int size=(int)n;
long[] time=new long[size];
long[] get_tension=new long[size];
long[] final_tension=new long[size];
for(int j=0;j<size;j++)
{
time[j]=sc.nextLong();
//System.out.println(time[j]);
}
for(int j=0;j<size;j++)
{
get_tension[j]=sc.nextLong();
//System.out.println(get_tension[j]);
}
for(int j=0;j<size;j++)
{
if(j==0)
{
final_tension[j]=get_tension[j];
}
else if(j>0)
{
final_tension[j]=get_tension[j]+final_tension[j-1]-(time[j]-time[j-1])*r;
}
}
/long max=final_tension[0];
for(int j=0;j<size;j++)
{
if(max<final_tension[j])
{
max=final_tension[j];
}
}
/
long count=0;
if(final_tension[size-1]<count)
output.add(count);
else
output.add(final_tension[size-1]);
}
for(int i=0;i<t;i++)
{
System.out.println(output.get(i));
}
}

}

}
Can Anyone tell mw what is wrong in my code ?
It corrects on test cases which are given .

ummm ohkay. See you are just calculating the tension at the end but we are to calculate the tension stepwise and see at which step it becomes maximum.
Consider the input:
2 1
1 500
500 1
Here when he does first workout tension becomes 500 but since there is 500 minute gap in the end tension is 1 which is less than 500 so final tension is not always the answer. Hope this helps

WHATS ISSUE WITH MY CODE
#include

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

int main() {
// your code goe
int t;
cin>>t;
while(t–)
{
long long int n,r ;
cin>>n>>r;

      long long int *ex=new   long long int[n];
      
      for( long long int i=0;i<n;i++)
      {
            cin>>ex[i];
      }
      
       long long int *te=new  long long int[n];
      
      for( long long int i=0;i<n;i++)
      {
            cin>>te[i];
      }
      
      
      
    
       long long int ans=0;
      
       int max=0;
      
       
       
       max=te[0];
      for(  long long int i=1;i<n;i++)
      {  
           ans=max-(r*(ex[i]-ex[i-1]))+te[i];
           
           
          if(ans>=max)
          {
              max=ans;
          }
            
         
          if(ans<0)
          {
              max=0;
          }
          
          
      }
       
          
          if(n==1)
       {
           ans=te[0];
       } 
      
      
      cout<<ans<<endl;
      
     
}
return 0;

}

ans=max(0ll,ans-temp*r);
CAN U PLEASE EXPLAIN THIS ONE

@thetufguy
First of all Thank you soo much for replying : )

but as per the test case which you gave
2 1
1 500
500 1
from 1 to 500 means 499 min time gap right??
because 500 - 1 is 499

b[i] - ( a[i+1 - a[i] ) * r + b[i+1]
b[0] - ( a[1] - a[0] ) * 1 + b[2]
500 - ( 500 - 1 ) * 1 + 1
500 - 499*1 + 1
500 -499 +1
1 + 1= 2 //ans

this above mentioned calculation is same as what codechef provided while explaining the test cases.

so to prove which ans is correct i just searched in the submission list for the correct ans user’s code who also used c++17 and the same approach which you mentioned and i found the code of @akashkoduru just to check what is the ans? 2 or as you mentioned 1 and the ans which i got is 500.

i mean how is this possible ?
is there still something which needs to be taken care of?

note:- since this is my first discussion in codechef i dont know whether it is ok to point
out about other users code, if it is not then please say i would not repeat that
again. but i would be thankfull if both of you help me in clearing my doubt.

thank you again : )

That’s what I said. The answer should be 500.
We have to give the maximum tension that he achieves at any point during his whole workout and not only at the end.
IN my test cases, let’s go step-wise.
He starts workout at t=1
at t=1 tension=500
Now he rests for 499 mins at the rate of one, so,
at t=500 tension=500-499=1
so there you go, you have seen the tension at all times when he does workout. Now all you have to do is to see which tension of all these is maximum. In our case, it is 500, which is at t=1;
Consider another case
3 2
1 2 3
5 6 1
here at t=1 tension=5
at t=2 tension =5-2*(2-1)+6=9
at t=3 tension =9-2*(3-2)+1=8
so here also maximum tension is found at t=2 and not at the end.
I hope you understand now : )

@thetufguy
yes i understood now
once again thank you soo much for replying and clearing my doubt
i have coded for same and ans got accepted
my code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int t;
cin >> t;
while (t–)
{
ll n, r;
cin >> n >> r;
ll a[n];
ll b[n];
for (ll i = 0; i < n; i++)
{
cin >> a[i];
}
for (ll i = 0; i < n; i++)
{
cin >> b[i];
}
ll tension = b[0];
ll max_tension = tension;
ll x = 0;
for (ll i = 1; i < n; i++)
{
tension = max(tension - (a[i] - a[i - 1]) * r, x) + b[i];
max_tension = max(max_tension, tension);
}
if (max_tension >= 0)
cout << max_tension << endl;
}
return 0;
}

1 Like

can someone pls tell error guys my code had passed sample case but on submitting it shows wrong answer.

#include
using namespace std;
#include <bits/stdc++.h>
int main() {
long long int n;
cin>>n;
for(long long int i = 0;i<n;i++){
long long int a,r;
cin>>a >>r;
long long int c[a];
long long int d[a];
for(long long int j = 0;j<a;j++){
cin>>c[j];
}
for(long long int k = 0;k<a;k++){
cin>>d[k];
}
long long int e[a];
e[0] = d[0];
for(long long int l = 1;l<a;l++){
long long int time = (c[l] - c[l-1]);
long long int relax = time*r;
long long int total = 0;
if(relax >= d[l-1]) total = 0 + d[l];
else total = d[l-1] - relax + d[l];
e[l] = total;
}
sort(e,e + a);
cout<<e[a-1]<<endl;
}
return 0;
}

MY APPROACH

#include
using namespace std;

int main() {
int t,n,r;
std::cin >> t;

for(int i=0;i<t;i++)
{
int x=0;
int max=0;
int loss=0;

cin>>n>>r;
int *A = new int(n);
int *B= new int(n);

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

for(int i=0;i<n;i++)
{
   x=x+B[i]; 
}

for(int i=0;i<n-1;i++)
{
    loss=loss+(A[i+1]-A[i])*r;
}

cout<< (x - loss) <<endl;

}
return 0;
}

not getting correct output!!
someone help

lets say n=3,
so when n=2 in forloop a[i+1] = a[3] which is not exist

#include <stdio.h>
int main()
{
int t;
scanf("%d", &t);
while (t–)
{
long int n;
long int r;
long long int total = 0;
long long int res = 0;
scanf("%ld", &n);
scanf("%ld", &r);
long long int min[n], ten[n];
for (long int i = 0; i < n; i++)
{
scanf("%lld", &min[i]);
}
for (long int i = 0; i < n; i++)
{
scanf("%lld", &ten[i]);
}
for (long int i = 0; i < n; i++)
{
total += ten[i];

        if (i >= 1)
        {

            total -= (r * (min[i] - min[i - 1]));
            if (total < 0)
            {
                total = 0;
            }
        }
        if (total > res)
        {
            res = total;
        }
    }

    printf("%lld\n", res);
}
return 0;

}

why it is giving a wrong answer?
can someone help,plzz?

can anyone point me where i am getting it wrong.
#include<bits/stdc++.h>
#include
using namespace std;
typedef long long ll;
#define moda 1000000007
//#define long long long long
//#define area 3.14rr
#define square(x) xx
#define all(x) x.begin(),x.end()
#define fldiv (1.0
x)/(1.0*y)

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen(“input.txt”, “r”, stdin);
// for writing output to output.txt
freopen(“output.txt”, “w”, stdout);
#endif

ll t;
cin>>t;
while(t--)
{

ll n,r;
cin>>n>>r;

ll tot=0;
ll breaks=0;

vector<ll> val(n,0);


vector<ll> tension(n,0);

for(ll i=0;i<n;i++)
{
    cin>>val[i];
    
}

//tot=0;
for(ll i=0;i<n;i++)
{
    cin>>tension[i];
    
}
ll x=0;

for(ll i=1;i<n;i++)
{
    breaks=val[i]-val[i-1];
    x=(tension[i-1]-r*breaks);
    x=max(x,0ll);
    tot+=x%moda;
    
}   
tot+=tension[n-1];


//cout<<breaks;
cout<<tot<<endl;




}
  return 0;
}

cook your dish here

for _ in range(int(input())):
n, r = list(map(int, input().split()))
time = list(map(int, input().split()))
tension = list(map(int, input().split()))

totStress = 0

for i in range(n):
    if i == 0:
        totStress += tension[i]
        
    else:
        totStress -= (time[i] - time[i-1]) * r
        totStress += tension[i]
        
print(totStress)

can someone help me what is wrong with my code ?