#include "souvenirs.h"
#include <bits/stdc++.h>
#include <utility>
#include <vector>
using namespace std;
pair <vector<int>, long long> transaction(long long M);
void buy_souvenirs(int N, long long P0) {
	pair <vector<int>, long long> res;
	if ( N == 2 ) {
		transaction ( P0 - 1 );
	} else if ( N == 3 ) {
		res = transaction ( P0 - 1 );
		if ( res.first.size() == 1 ) {
			transaction ( P0 - 1 - res.second - 1 );
			transaction ( P0 - 1 - res.second - 1 );
		} else {
			transaction ( ( P0 - 1 - res.second ) / 2 );
		}
	} else if ( P0 == N ) {
		for ( int i = 1; i < N; i++ ) {
			for ( int j = 0; j < i; j++ ) {
				transaction ( N - i );
			}
		}
	} else {
		pair <long long, long long> a[N];
		for ( int i = 0; i < N; i++ ) {
			a[i].first = 0;
			a[i].second = 0;
		}
		a[0] = { P0, 1 };
		for ( int i = 1; i < N; i++ ) {
			if ( i == N - 1 ) {
				while ( a[i].second < i ) {
					transaction( a[i - 1].first - 1 );
					a[i].second++;
				}
			}
			res = transaction( a[i - 1].first - 2 );
			if ( res.first.size() == 1 ) {
				if ( res.first[0] == i ) {
					a[i].first = a[i - 1].first - 2;
					a[i].second++;
					while ( a[i].second < i ) {
						transaction( a[i].first );
						a[i].second++;
					}
				} else {
					a[i].first = a[i - 1].first - 1;
					a[i + 1].second++;
					while ( a[i].second < i ) {
						transaction( a[i].first );
						a[i].second++;
					}
				}
			} else {
				a[i].first = a[i - 1].first - 1;
				a[i + 1].second++;
				a[N - 1].second++;
				while ( a[i].second < i ) {
					transaction( a[i].first );
					a[i].second++;
				}
			}
		}
	}
	return;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |