#include "souvenirs.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn=102;
long long cost[maxn],freq[maxn];
int n;
void solve(long long brp){
//cout<<brp<<endl;
pair<vector<int>,long long>hmm=transaction(brp);
vector<int>brg=hmm.first; long long sisa=hmm.second;
for(auto x : brg)freq[x]++;
brp-=sisa;
while(true){
long long blm=0,tmp=brp;
vector<int>mana;
for(auto x : brg){
if(cost[x]!=-1)tmp-=cost[x];
else {
mana.push_back(x); blm++;
}
}
// cari yg terkecil biar ga tau cost[brg[0]]
if(blm==1){
cost[mana[0]]=tmp; break;
}
solve(tmp/blm);
}
for(auto q : brg){
if(q!=n-1 && cost[q+1]==-1)solve(cost[q]-1);
}
}
void buy_souvenirs(int N, long long P0) {
memset(cost,-1,sizeof cost);
n=N;
cost[0]=P0;
solve(P0-1);
for(int q=1;q<N;q++){
while(freq[q]<q){
transaction(cost[q]); freq[q]++;
}
}
return;
}