How to approach STUPMACH?

I couldn’t work out a solution for this, can anyone please share their approach?

See if this helps : https://www.codechef.com/viewsolution/28547930

Case 1 : Let’s say you are at 10th box whose Si = 20 and minimum Si found till Box 1 —> 9, say = 11, then you cannot put more than 11 tokens in 10th box, so, 10th box will hold minimum found so far

Case 2 : Let’s say you are at 10th box whose Si = 6 and minimum Si found till Box 1 —> 9, say = 11, then you cannot put more than 6 tokens in 10th box, so, 10th box will 6 tokens. Hence going foward, irrespective of say 11th box value, you will not reach 11th box more than 6 times.

Repeat the process.

1 Like

Did you get it bro?

Yes it did make so much sense now, I should’ve thought of it myself! Thanks for the insight brother, much appreciated.

1 Like

Yes it did make so much sense now, I should’ve thought of it myself! Thanks for the insight brother, much appreciated.

i got 50 marks in this prob
should i share mt code / algo?

yes you can

See if this is helpful to anyone:

https://www.codechef.com/viewsolution/28544348

Hey bro, You have written so much code for a question which can be solved in 4 to 5 lines. Can you pl check my solution. Hope it helps!

1 Like

My code was doing the same, can you just check if my logic was correct?
#include <bits/stdc++.h>
using namespace std;
std::unordered_map<string,int> freq;
typedef long long int ll;

// Driver code
int main()
{
ll t;
cin>>t;
for(int i=0;i<t;i++)
{
ll n;
cin>>n;
ll arr[n];
for(int j=0;j<n;j++)
{
cin>>arr[j];

    }
    ll e,pos=0,l;

        for(int j=0;j<n-1;j++)
        {   //cout<<"gussa2"<<endl;
            if(arr[j+1]<arr[j])
            {
                e=arr[j+1];
                pos=j+1;
                
            }
        }
        if(pos==0)
        {
            l=arr[0]*n;
            cout<<l<<endl;
        }
        else if(pos==n-1)
        { ll sum=0;
            for(int j=0;j<n;j++)
            {   
                sum+=arr[j];
            }
            cout<<sum<<endl;
        }
        else
        {
            l=n*e+pos;
        cout<<l<<endl;
        }
        
    }
}

https://www.codechef.com/viewsolution/28535119
another short solution

1 Like