BODYBUILDER, WHERE WAS THE MISTAKE?

#include<bits/stdc++.h>
using namespace std;
main(){

long int T;
cin>>T;
while(T–){
long int N,R;
cin>>N;
cin>>R;
long int A[N];
long int B[N];
for(long int i=0;i<N;i++){
cin>>A[i];
}
for(long int i=0;i<N;i++){
cin>>B[i];
}
long int x=0;
long int maxt=0;
long int currt=0;
for(long int i=0;i<N;i++){

        if(i==N-1){
            currt=B[i]+currt;
        }
        else{
         currt=B[i]-(A[i+1]-A[i])*R+currt;

        }
        
        if(currt>maxt){
            maxt=currt;
        }
        if(currt<0){
            currt=0;
        }
    }
    cout<<maxt<<"\n";
}

}

The for loop you are running is from 0 to N-1
but A[i+1] will give A[N] for i=N-1
and A[N] will surely give garbage value

THANK YOU SO MUCH…It means alot for spending your precious time on my problem

Still it is showing wrong answer, can u please tell me, why it is not getting accepted?

#include<bits/stdc++.h>
using namespace std;
main(){
   
   long int T;
    cin>>T;
    while(T--){
         long int N,R;
        cin>>N;
        cin>>R;
        long int A[N];
        long int B[N];
        for(long int i=0;i<N;i++){
            cin>>A[i];
        }
        for(long int i=0;i<N;i++){
            cin>>B[i];
        }
        
        long int maxt=0;
        long int currt=0;
        for(long int i=0;i<N-1;i++){
           
           
         
            
             currt=B[i]-(A[i+1]-A[i])*R+currt;

            
            
            if(currt>maxt){
                maxt=currt;
            }
            if(currt<0){
                currt=0;
            }
        }
        maxt=maxt+B[N-1];
        
        cout<<maxt<<"\n";
    }
}

for my python users

for _ in range(int(input())):
n,trpm = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ct,mt=b[0],b[0]
for i in range(1,n):
ct = ct - trpm*(a[i]-a[i-1])
ct = max(ct,0)+b[i]
mt = max(mt,ct)
print(mt)

1 Like

format your code

your while is using wrong syntaxes. The correct syntax should be
while(T--)

See if you are checking curr<0 after adding b[i]…You should check the factor (a[i+1]-a[i])*R should not be negative …
for(long int i=0;i<N-1;i++)
{
long long int x;
if((A[i+1]-A[i])*R <=0 )
{
x= 0;
}
else
{
x=(A[i+1]-A[i])*R;
}
currt=B[i]-x+currt;

        if(currt>maxt)
        {
            maxt=currt;
        }

}

you need to maximize the release in tension with zero.
max(0, r*(a[i+1]-a[i]).
dont release the tension at very last iteration

1 Like

Hey mate, I think the input is not correct here. Since, the condition is A[i-1] < A[i], but here you mentioned 4 6 6 8. Clearly A[1]=A[2] here.

Yeah you’re right. Thanks for pointing out. But this code still fails on

1
2 1
1 9
6 1

Correct output is 6 but his code gives 1

Hey mate, Thanks for asking. According to the problem we have to find the max tension, the chef will face during his entire workout. So, we have to check whether current tension is also the max tension or not.

#include<bits/stdc++.h>
using namespace std;
main(){

long int T;
cin>>T;
while(T–){ //consider T - -;
long int N,R;
cin>>N;
cin>>R;
long int A[N];
long int B[N];
for(long int i=0;i<N;i++){
cin>>A[i];
}
for(long int i=0;i<N;i++){
cin>>B[i];
}
long long maxt=0;
long long currt=0;
for(long int i=0;i<N;i++){

   currt = B[i] + currt; //Finding current tension also
    if(currt>maxt){
        maxt=currt; 
     /*Checking if it is greater than max tension during his entire workout */
    }
    if(i!=N-1){
     currt=currt-(A[i+1]-A[i])*R; //tension after rest.
    }
    if(currt<0){
        currt=0;
    }

}
cout<<maxt<<"\n";
}
}

Consider this input

1
2 3
1 3
2 1

After doing the first workout the tension is 2
currt = currt+B[i] =2 and maxt = 2
After doing the first workout, the chef took rest. So, Next Step,
currt = currt - (A[i+1]-A[i])*R = 3-(3-1)*2= -3 <0. So, now tension = 0, after his first workout.
After doing the second workout the tension is 1
currt = currt + B[i] = 0 + 1 = 1 and maxt = 2 (Since, 2>1)

Conclusion/Output: So, the maximum tension he will be feeling in his muscles during the entire period of his workout will be "2"

But your code is displaying 1, which is not the correct output.

Yep, I have found the solution for this mate, that we have to consider the tension before rest as well.

Sorry to bother you but your input is incorrect.

I’ve addressed it here already

I didn’t go down . I found your reply. Found the mistake and that’s why thought to correct it.

Thanks to all of you… I will correct and submit

CAN YOU PLEASE HELP ME IN SOLVING MY DOUBT??

Y 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
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

#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 = 0;
for (ll i = 0; i < n; i++)
{
tension = tension + b[i];
}
ll rest = 0;
for (ll i = 1; i < n; i++)
{
rest = rest + r * (a[i] - a[i - 1]);
}
ll final_tension = 0;
final_tension = tension - rest;
if (final_tension < 0)
cout << 0 << endl;
else
cout << final_tension << endl;
}
return 0;
}

NOTE:-I HAVE FORMATTED MY CODE BUT ITS NOT GETTING APPLIED SORRY ABOUT THAT… :frowning:

#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?