# GMBLE - Editorial

Contest

Author: Bhavya Dhingra
Tester: Syed Ali Aatif
Editorialist: Prateek Mishra, Naman Dhingra

EASY

Array

# EXPLANATION

For each test case, first the final outcome has to be checked, whether the team batting will be able to score r runs or not. For this, calculate the sum of the given sequence S (denoted by Sum) and compare it with r. Then there can be 3 outcomes -
Sum >= r : In this case, the team batting will be able to score at least r runs in the n balls. To calculate the profit, we have to find out at what number of balls is the sum >= r. For this, start from the first ball and keep adding the runs, as soon as it is >= r, store the number of balls left. Then the profit is 50 * ( number of balls left ) - b.
Sum = r-1 : In this case, the team batting will tie with the other team hence the profit will be 0
Sum < r-1 : In this case, the team batting is not able to win. To calculate the profit, we have to find, at what ball is it impossible to win? Start from the first ball and check if runs left to score > 6*balls left. If yes, then it is impossible to win and profit is 50 * ( n - i ) - a

Example 1

Suppose Chef decides to place equal bets on both teams A and B. Let’s suppose this bet accounts to Rs.400 for each team. Also, currently, team A requires 10 runs from the last 6 balls to win. We provide input in the form of 400 400 6 10, giving us a = 400, b = 400, n = 6 and r = 10. Let the runs scored by team A in n = 6 balls be 1, 0, 0, 2, 4, 4. If we find the sum of runs, it comes out to be 11.That means team A will win the match at the last ball. Now, according to the given formula, the profit/loss incurred by Chef will be = 50-400. Since profit/loss incurred is negative, Chef incurs a net loss of Rs.350 . So, the desired output will be :

``````Loss 350
``````
Example 2

Chef bets Rs.400 on team A and Rs.200 on team B. Currently, team A requires 20 runs from the last 4 balls to win. We provide input in the form of 400 200 4 20, giving us a=400, b=200, n=4 and r=20. Let runs scored by team A in last n=4 balls be 6, 6, 0 , 1 respectively. If we find the sum of runs scored, it comes out to be 6+6+0+1=13.That means team A will lose the match and team B will win.
Also, it is IMPORTANT to note that, in the last 2 balls, team A needed to score at-least 8 runs to win, but scored 0 runs in 2nd last ball, thus needing to score 8 runs in the last ball, which is impossible. Thus, team B wins in the 2nd last ball of the game.
Now, total profit/loss incurred by Chef will be = ( 50 * 2 ) - 400= -300. Since profit/loss incurred is negative, Chef incurs a total loss of Rs.300. So, the desired output will be :

``````Loss 300
``````

# SOLUTION:

Setter’s Solution
``````#include <bits/stdc++.h>
using namespace std;

#define sync {ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
#define rep(n) for(int i = 0;i<n;i++)
#define rep1(a,b) for(int i = a;i<b;i++)
#define int long long int
#define mod 1000000007

int a,b,n,r;

void solve()
{
cin>>a>>b>>n>>r;
int runs[n];
rep(n)
cin>>runs[i];
int sum = 0;
int refund = LLONG_MIN;
for(int i = 0;i<n;i++)
{
if(r-sum>6*(n-i) ){
refund = max(50*(n-i) - a,refund);
}
if(sum>=r)
{
refund = max(50*(n-i) - b,refund);
}
sum += runs[i];
}

if(sum>=r)
{
refund = max(refund,-b);
}
else if(sum<r-1)
{
refund = max(refund,-a);
}
else
{
refund = 0;
}
if(refund>0)
{
cout<<"Profit "<<refund;
}
else if(refund<0)
{
cout<<"Loss "<<abs(refund);
}
else
{
cout<<"No profit no loss";
}
}

int32_t main()
{

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

sync;
int t = 1;
cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
``````
1 Like