#include "souvenirs.h"
#include <utility>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define lsb(x) (x&(-x))
#define pii pair<int,int>
#define ss second
#define ff first
#define piii pair<int,pii>
#define debu(x) (cerr << #x << " = "<< x << "\n")
#define debu2(x,y) (cerr << #x << " = "<< x << " " << #y << " = " << y << "\n")
#define debu3(x,y,z) (cerr << #x << " = "<< x << " " << #y << " = " << y << " " << #z << " = " << z<< "\n")
#define bitout(x,y) {\
cerr << #x << " : ";\
for (int justforbits = y; justforbits >=0; justforbits--)cout << (((1 << justforbits) & x)>=1);\
cout << "\n";\
}
#define rangeout(j,rangestart,rangeend) {\
cerr << "outputting " << #j<< ":\n";\
for (int forrang = rangestart; forrang <= rangeend; forrang++)cerr << j[forrang] << " ";\
cerr<<"\n";\
}
#define c1 {cerr << "Checkpoint 1! \n\n";cerr.flush();}
#define c2 {cerr << "Checkpoint 2! \n\n";cerr.flush();}
#define c3 {cerr << "Checkpoint 3! \n\n";cerr.flush();}
#define c4 {cerr << "Checkpoint 4! \n\n";cerr.flush();}
#define vi vector<int>
#define vpii vector<pii>
#define fr(i,x,y) for(int i=x;i<=y;i++)
//1: just do P[0]-1
//2: literally just go shopping like a normal person
//3: keep going (prev price)-1 => if u keep buying a little guy its ok => at each point u need to solve
void buy_souvenirs(int N, long long P0)
{
//find ur price => {me,1} or {me, small guy}
//else ur just a minus one
//doesn't matter what small guy is => just keep getting?? => what if u accidentally get 2 :(
//=> make sure to mark it out if small guy is just 1 !
ll n=N;
vector<ll>cnt(n,0);
bool sgio=false;
ll curr=P0;
for(ll i=1;i<n;i++)
{
if(sgio&&i==n-1)break;
auto x=transaction(curr-1);
if(x.ff.size()==2){sgio=true;curr=curr-2;}
else if(x.ss==1){curr=curr-2;}
else {curr=curr-1;}
for(ll y:x.ff){cnt[y]++;}
if(cnt[i]!=i){transaction(curr*(i-cnt[i]));}
}
//edge case for small guy
if(sgio){transaction(n-1-cnt[n-1]);}
return;
}