Bodybuilder Codechef Solution

Hello coders, today we are going to solve Bodybuilder Codechef Solution.

Bodybuilder Codechef Solution
Bodybuilder Codechef Solution

Problems

After solving programming problems for years, Chef has become lazy and decided to get a better physique by doing some weight lifting exercises.

On any regular day, Chef does NN exercises at times A1,A2,…,ANA1,A2,…,AN (in minutes, all distinct) and each exercise provides a tension of B1,B2,…,BNB1,B2,…,BN units. In the period between two consecutive exercises, his muscles relax RR units of tension per minute.

More formally, Chef’s tension is described by a number xx. Before any workouts, x=0x=0. When he does a workout at time AiAi, the tension xx instantly increases by BiBi. Between workouts, the number xx decreases by RR units per minute, maximized with 00.

Considering the time of exercise and hence tension to be negligible, find the maximum tension he will be feeling in his muscles during the entire period of his workout.

Input:

  • First line will contain TT, number of testcases. Then the testcases follow.
  • Each testcase contains 33 lines of input.
  • The first line will contain 22 space-separated integers N,RN,R, number of timestamps at which Chef performs his exercise, and units of tension relaxed per minute.
  • The second line contains NN space-separated integers A1,A2,…,ANA1,A2,…,AN.
  • The third line contains NN space-separated integers B1,B2,…,BNB1,B2,…,BN.

Output:

For each testcase, output in a single line the maximum amount of tension Chef will have in his muscles.

Constraints

  • 1≤T≤101≤T≤10
  • 1≤N≤5⋅1041≤N≤5⋅104
  • 1≤R,Bi≤1051≤R,Bi≤105
  • 1≤Ai≤1091≤Ai≤109
  • Ai−1<AiAi−1<Ai, for all 2≤i≤N2≤i≤N

Sample Input 1 

3
1 2
10
10
2 2
10 11
10 10
3 1
1 2 3
1 2 3

Sample Output 1 

10
18
4

Explanation

Test Case 1: Since there is only 11 exercise, the maximum tension is equal to the tension provided by that exercise, i.e, 1010 units.

Test Case 2: At time t=10t=10, Chef has 1010 units of tension.

From t=10t=10 to t=11t=11, his muscles releases 22 unit of tension and at t=11t=11, he further gains 1010 units of tension to have total of 10−2+10=1810−2+10=18 units of tension.

So the maximum tension Chef feels in his muscles is 1818 units.

Test Case 3: At time t=1t=1, Chef has 11 unit of tension.

From t=1t=1 to t=2t=2, his muscles releases 11 unit of tension and at t=2t=2, he further gains 22 units of tension to have total of 1−1+2=21−1+2=2 units of tension.

From t=2t=2 to t=3t=3, his muscles releases 11 unit of tension and at t=3t=3, he further gains 33 units of tension to have total of 2−1+3=42−1+3=4 units of tension.

So the maximum tension Chef feels in his muscles is 44 units.

Bodybuilder CodeChef Solution in JAVA

import java.util.*;
class Buildb
{
    public static void main(String args[])
    {
        try {
            Scanner in = new Scanner(System.in);
            int T=in.nextInt();
            while(T-->0)
            {
                int N=in.nextInt();
                long R=in.nextLong();
                long A[]=new long[N];
                long B[]=new long[N];
                long r=0;
                for(int i=0;i<N;i++)
                A[i]=in.nextLong();
                for(int i=0;i<N;i++)
                B[i]=in.nextLong();
                long t=0,m=0;
                for(int i=0;i<N;i++)
                {
                    t+=B[i];
                    if(m<t)
                    m=t;
                    if(i!=N-1)
                    t-=(A[i+1]-A[i])*R;
                    if(t<0)
                    t=0;
                }
                System.out.println(m);
            }
        } catch(Exception e) {
            return;
        }
    }
}

Bodybuilder CodeChef Solution in CPP

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,r;
        cin>>n>>r;
        ll a[n],b[n];
        for(ll i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(ll i=0;i<n;i++)
        {
            cin>>b[i];
        }
        ll sum=0;
        ll ans=0;
        for(ll i=0;i<n;i++)
        {
            sum+=b[i];
            ans=max(sum,ans);
            if(i!=n-1)
            {
                sum-=r*(a[i+1]-a[i]);
            }
            if(sum<0)
            {
                sum=0;
            }
        }
        cout<<ans<<"\n";
    }
}

Bodybuilder CodeChef Solution in Python

for _ in range(int(input())):
    n, r = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = temp = b[0]
    for i in range(1, n):
        temp -= (a[i] - a[i-1]) * r
        if temp < 0: temp = 0
        temp += b[i]
        if temp > ans: ans = temp
    print(ans)

Disclaimer: The above Problem (Bodybuilder) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring

Leave a Comment