#include "souvenirs.h"
#include <utility>
#include <vector>
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
void buy_souvenirs(int N, long long P0) {
int n = N;
vector<ll> p(N, -1);
p[0] = P0;
ll p0 = P0;
vector<int> amount_needed(N);
for(int i = 0; i < n; i++) amount_needed[i] = i;
for(int i = 1; i < n; i++){
if(amount_needed[i] == 0) continue;
pair<vector<int>, ll> buy = transaction(p[i - 1] - 1);
ll change = buy.second;
vector<int> items = buy.first;
amount_needed[i]--;
if(items.size() == 1){
p[i] = p[i - 1] - 1 - change;
}
else{
p[i] = p[i - 1] - 2;
p[n - 1] = 1;
amount_needed[n - 1] --;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < amount_needed[i]; j++){
transaction(p[i]);
}
}
}