Problem:
Problem I:
Sweet War
There are two countries, Imperial Cacao and Principality of Cocoa. Two girls, Alice (the Empress of Cacao) and Brianna (the Princess of Cocoa) are friends and both of them love chocolate very much.
One day, Alice found a transparent tube filled with chocolate balls (Figure I.1). The tube has only one opening at its top end. The tube is narrow, and the chocolate balls are put in a line. Chocolate balls are identified by integers 1, 2, . . ., $N$ where $N$ is the number of chocolate balls. Chocolate ball 1 is at the top and is next to the opening of the tube. Chocolate ball 2 is next to chocolate ball 1, . . ., and chocolate ball $N$ is at the bottom end of the tube. The chocolate balls can be only taken out from the opening, and therefore the chocolate balls must be taken out in the increasing order of their numbers.
Figure I.1. Transparent tube filled with chocolate balls
Alice visited Brianna to share the tube and eat the chocolate balls together. They looked at the chocolate balls carefully, and estimated that the $nutrition value$ and the $deliciousness$ of chocolate ball $i$ are $r_i$ and $s_i$, respectively. Here, each of the girls wants to maximize the sum of the deliciousness of chocolate balls that she would eat. They are sufficiently wise to resolve this
conflict peacefully, so they have decided to play a game, and eat the chocolate balls according to the rule of the game as follows:
Alice and Brianna have initial energy levels, denoted by nonnegative integers $A$ and $B$, respectively.
Alice and Brianna takes one of the following two actions in turn:
Pass
: she does not eat any chocolate balls. She gets a little hungry $-$ specifically, her energy level is decreased by 1. She cannot pass when her energy level is 0.
Eat
: she eats the topmost chocolate ball $-$ let this chocolate ball $i$ (that is, the chocolate ball with the smallest number at that time). Her energy level is increased by $r_i$, the nutrition value of chocolate ball $i$ (and NOT decreased by 1). Of course, chocolate ball $i$ is removed from the tube.
Alice takes her turn first.
The game ends when all chocolate balls are eaten.
You are a member of the staff serving for Empress Alice. Your task is to calculate the sums of deliciousness that each of Alice and Brianna can gain, when both of them play optimally.
Input
The input consists of a single test case. The test case is formatted as follows.
$N$ $A$ $B$
$r_1$ $s_1$
$r_2$ $s_2$
.
.
.
$r_N$ $s_N$
The first line contains three integers, $N$, $A$ and $B$. $N$ represents the number of chocolate balls. $A$ and $B$ represent the initial energy levels of Alice and Brianna, respectively. The following $N$ lines describe the chocolate balls in the tube. The chocolate balls are numbered from 1 to $N$, and each of the lines contains two integers, $r_i$ and $s_i$ for $1 \leq i \leq N$. $r_i$ and $s_i$ represent the nutrition value and the deliciousness of chocolate ball $i$, respectively. The input satisfies
$1 
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(ll i=0;i<n;i++)

int main(){
    ll N,A,B,D=155,an=1e18;
    cin>>N>>A>>B;
    vector<ll> R(N),S(N),SumS(N+1,0);
    rep(i,N){
        cin>>R[i]>>S[i];
        SumS[i+1]=SumS[i]+S[i];
    }
    vector<vector<ll>> DP(N+1,vector<ll>(D*2+1,1e18));
    rep(i,D+1)DP[N][i]=DP[N][i]=-1e18;
    
    for(int n=N-1;n>=0;n--){
        ll Z=SumS[N]-SumS[n];
        rep(i,2*D+1){
            ll x=Z-i+1+D*2;
            if(0<=x&&x<D*2+1)DP[n][i]=min(DP[n][i],-DP[n+1][x]-R[n]+1);
            DP[n][i]=min(DP[n][i],max(DP[n+1][i]+1+R[n],1ll));
            for(int i=D*2-1;i>=0;i--)DP[n][i]=min(DP[n][i],DP[n][i+1]);
        }
    }
    rep(i,2*D+1)if(DP[0][i]<=A-B)an=i-D;
    cout<<an<<" "<<(SumS[N]-an)<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3760
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(ll i=0;i<n;i++)

int main(){
    ll N,A,B,D=310,an=1e18;
    cin>>N>>A>>B;
    vector<ll> R(N),S(N),SumS(N+1,0);
    rep(i,N){
        cin>>R[i]>>S[i];
        SumS[i+1]=SumS[i]+S[i];
    }
    vector<vector<vector<ll>>> DP(N+1,vector<vector<ll>>(D*2+1,vector<ll>(2,1e18)));
    rep(i,D+1)DP[N][i][0]=DP[N][i][1]=-1e18;
    
    for(int n=N-1;n>=0;n--){
        ll Z=SumS[N]-SumS[n];
        rep(i,2*D+1){
            ll x=Z-i+1+D*2;
            if(0<=x&&x<D*2+1)for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],-DP[n+1][x][1-t]-R[n]+1);
            rep(t,2)DP[n][i][t]=min(DP[n][i][t],max(DP[n+1][i][t]+1+R[n],1ll));
            for(int i=D*2-1;i>=0;i--)for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],DP[n][i+1][t]);
        }
    }
    rep(i,2*D+1)if(DP[0][i][0]<=A-B)an=i-D;
    cout<<an<<" "<<(SumS[N]-an)<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 8540
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
    
    ll N,A,B,D=310,an=1e18;
    cin>>N>>A>>B;
    vector<ll> R(N),S(N),SumS(N+1,0);
    for(int i=0;i<N;i++){
        cin>>R[i]>>S[i];
        SumS[i+1]=SumS[i]+S[i];
    }
    vector<vector<vector<ll>>> DP(N+1,vector<vector<ll>>(D*2+1,vector<ll>(2,1e18)));
    for(int i=0;i<D*2+1;i++)if(i<=D)DP[N][i][0]=DP[N][i][1]=-1e18;
    
    for(int n=N-1;n>=0;n--){
        ll Z=SumS[N]-SumS[n];
        for(int i=0;i<D*2+1;i++){
            ll x=Z-i+1+D*2;
            if(0<=x&&x<D*2+1)for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],-DP[n+1][x][1-t]-R[n]+1);
            for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],max(DP[n+1][i][t]+1+R[n],1ll));
            for(int i=D*2-1;i>=0;i--)for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],DP[n][i+1][t]);
        }
    }
    for(int i=0;i<D*2+1;i++)if(DP[0][i][0]<=A-B)an=i-D;
    cout<<an<<" "<<(SumS[N]-an)<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 8608
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
    
    ll N,A,B;
    cin>>N>>A>>B;
    vector<ll> R(N),S(N),SumS(N+1,0);
    R.resize(N);
    S.resize(N);
    SumS.assign(N+1,0);
    for(int i=0;i<N;i++){
        cin>>R[i]>>S[i];
        SumS[i+1]=SumS[i]+S[i];
    }
    int BASE=310;
    vector<vector<vector<ll>>> DP(N+1,vector<vector<ll>>(BASE*2+1,vector<ll>(2,1e18)));
    for(int i=0;i<BASE*2+1;i++)if(i<=BASE)DP[N][i][0]=DP[N][i][1]=-1e18;
    
    for(int n=N-1;n>=0;n--){
        ll Z=SumS[N]-SumS[n];
        for(int i=0;i<BASE*2+1;i++){
            ll x=i-BASE;
            x=Z-x+1+BASE;
            if(0<=x&&x<BASE*2+1)for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],-DP[n+1][x][1-t]-R[n]+1);
            for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],max(DP[n+1][i][t]+1+R[n],1ll));
            for(int i=BASE*2-1;i>=0;i--)for(int t=0;t<2;t++)DP[n][i][t]=min(DP[n][i][t],DP[n][i+1][t]);
        }
    }
    ll an=-1e18;
    for(int i=0;i<BASE*2+1;i++)if(DP[0][i][0]<=A-B)an=i-BASE;
    cout<<an<<" "<<(SumS[N]-an)<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 8612
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, x) for (auto &e : x)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()
#define eb emplace_back
#define MM << ' ' <<
#define TT template <typename T>
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

TT void print(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (n == 0) cout << '\n';
}

TT void printn(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << '\n';
}

TT void rearrange(vector<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), end(v));
}

TT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }
TT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main() {
    int N;
    ll A, B;
    cin >> N >> A >> B;

    vector<ll> a(N), b(N);
    rep(i, N) cin >> a[i] >> b[i];

    vector<ll> s(N + 1, 0);
    rep(i, N) s[i + 1] = s[i] + b[i];

    int L = 150;

    vector<vector<ll>> dp(N + 1, vector<ll>(L + 1, INF));
    dp[N][0] = -INF;

    auto get = [&](int i, ll x, bool isFirst) {
        int tmp = ub(dp[i], x) - 1;
        return isFirst ? tmp : s[N] - s[i] - tmp;
    };

    ll D = (1LL << 40) - 1;

    per(i, N) {
        rep(j, L + 1) {
            ll ok = D, ng = -D - 1;

            auto judge = [&](ll x) {
                if (x > 0) {
                    ll nx = x - 1 - a[i];
                    if (get(i + 1, nx, true) >= j) return true;
                }
                {
                    ll nx = x + a[i];
                    if (get(i + 1, -nx, false) + b[i] >= j) return true;
                }
                return false;
            };

            while (abs(ok - ng) > 1) {
                ll mid = (ok + ng) / 2;
                (judge(mid) ? ok : ng) = mid;
            }

            dp[i][j] = (ok == -D ? -INF : ok == D ? INF : ok);
        }
    }

    ll ans = get(0, A - B, true);

    cout << ans MM s[N] - ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 4

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 155

int N;
ll A,B;

struct Info{
	ll nut_value,sweet_value;
};
Info info[NUM];
ll sweet_sum;
ll dp[NUM][NUM];

ll get_max(int index, ll diff){

	ll ret = 0;

	ll left = 0,right = sweet_sum,mid = (left+right)/2;

	while(left <= right){

		if(dp[index][mid] <= diff){

			ret = mid;
			left = mid+1;
		}else{
			right = mid-1;
		}
		mid = (left+right)/2;
	}

	return ret;
}

bool can_get(int index,ll diff,ll tmp_sweet_sum){

	ll eat_value = sweet_sum-get_max(index+1,-(diff+info[index].nut_value));
	ll not_value;

	if(diff > 0){

		not_value = get_max(index+1,diff-1-info[index].nut_value);

	}else{

		not_value = 0;
	}

	return tmp_sweet_sum <= max(0ll,max(eat_value,not_value));
}


int main(){

	scanf("%d %lld %lld",&N,&A,&B);

	sweet_sum = 0;

	for(int i = 0; i < N; i++){

		scanf("%lld %lld",&info[i].nut_value,&info[i].sweet_value);
		sweet_sum += info[i].sweet_value;
	}

	for(int i = 0; i <= N; i++){
		dp[i][0] = 0;
		for(int k = 1; k <= sweet_sum; k++){
			dp[i][k] = HUGE_NUM;
		}
	}

	sweet_sum = 0;
	ll left,right,mid,min_diff;

	for(int i = N-1; i >= 0; i--){

		sweet_sum += info[i].sweet_value;

		for(int tmp_sweet = 0; tmp_sweet <= sweet_sum; tmp_sweet++){

			left= -HUGE_NUM,right = HUGE_NUM,mid = (left+right)/2;
			min_diff = HUGE_NUM;

			while(left <= right){
				if(can_get(i,mid,tmp_sweet)){

					min_diff = mid;
					right = mid-1;

				}else{

					left = mid+1;
				}
				mid = (left+right)/2;
			}
			dp[i][tmp_sweet] = min_diff;
		}
	}

	ll ans_A = get_max(0,A-B);
	ll ans_B = sweet_sum-ans_A;

	printf("%lld %lld\n",ans_A,ans_B);

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3408
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef long long ll;
ll dp[151][152],inf=1e18;
int N;
ll A,B,r[150],s[150];
int lef;
int geteasy(int i,ll x){
	return upper_bound(dp[i],dp[i]+151,x)-dp[i]-1;
}
int get(int i,ll x){
	int ret=0;
	chmax(ret,lef-geteasy(i+1,-x-r[i]));
	if(x>0){
		chmax(ret,geteasy(i+1,x-1-r[i]));
	}
	return ret;
}
int main(){
	int N;
	cin>>N>>A>>B;
	rep(i,N) cin>>r[i]>>s[i];
	rep(i,151) rep(j,152) dp[i][j]=inf;
	dp[N][0]=-inf;
	for(int i=N-1;i>=0;i--){
		lef+=s[i];
		rep(j,lef+1){
			ll ub=inf,lb=-inf;
			while(ub-lb>1){
				ll m=(ub+lb)/2;
				if(get(i,m)>=j) ub=m;
				else lb=m;
			}
			dp[i][j]=ub;
		}
	}
	ll x=geteasy(0,A-B);
	cout<<x<<" "<<lef-x<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 1352
final_rank: 1

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int r[200];
int s[200];
long long dp[200][200][2];
int v[200][200][2];
int n;
long long INF=99999999999999LL;
int sum;
long long solve(int a,int b,int c){
	if(v[a][b][c])return dp[a][b][c];
	v[a][b][c]=1;
	if(a==n){
		if(b==0){
			if(c==0)dp[a][b][c]=-INF;
			else dp[a][b][c]=INF;
		}else{
			if(c==0)dp[a][b][c]=INF;
			else dp[a][b][c]=-INF;
		}
	//	printf("%d %d %d: %lld\n",a,b,c,dp[a][b][c]);
		return dp[a][b][c];
	}
	if(c==0)dp[a][b][c]=INF;
	else dp[a][b][c]=-INF;
	if(b-s[a]*(1-c)>=0){
		//long long t=solve(a+1,b-s[a]*(1-c),!c);
		if(c==0){
			long long t=-INF;
		//	printf("%d\n",b-s[a]);
			for(int i=0;i<b-s[a];i++){
				//if(i>sum)printf("ng\n");
				t=max(t,solve(a+1,i,1)+1);
			}
			dp[a][b][c]=min(dp[a][b][c],t-r[a]);
		}else{
			long long t=INF;
			for(int i=b+1;i<=sum;i++)t=min(t,solve(a+1,i,0)-1);
			dp[a][b][c]=max(dp[a][b][c],t+r[a]);
		}
	}
	if(b-s[a]*c>=0){
		long long t=solve(a+1,b-s[a]*c,c);
		if(c==0){
			t=max(t+r[a],0LL);
			dp[a][b][c]=min(dp[a][b][c],t+1);
		}else{
			t=min(t-r[a],0LL);
			dp[a][b][c]=max(dp[a][b][c],t-1);
		}
	}
//	printf("%d %d %d: %lld\n",a,b,c,dp[a][b][c]);
	return dp[a][b][c];
}
int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	n=a;
	for(int i=0;i<a;i++)scanf("%d%d",r+i,s+i);
	sum=0;
	for(int i=0;i<a;i++)sum+=s[i];
	for(int i=sum;i>=0;i--){
		if(b-c>=solve(0,i,0)){
			printf("%d %d\n",i,sum-i);return 0;
		}
	}
	
}
accuracy: 1
time_ms: 20
memory_kb: 1756
final_rank: 2

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int r[200];
int s[200];
long long dp[200][200][2];
int v[200][200][2];
int n;
long long INF=99999999999999LL;
int sum;
long long solve(int a,int b,int c){
	if(v[a][b][c])return dp[a][b][c];
	v[a][b][c]=1;
	if(a==n){
		if(b==0){
			if(c==0)dp[a][b][c]=-INF;
			else dp[a][b][c]=INF;
		}else{
			if(c==0)dp[a][b][c]=INF;
			else dp[a][b][c]=-INF;
		}
	//	printf("%d %d %d: %lld\n",a,b,c,dp[a][b][c]);
		return dp[a][b][c];
	}
	if(c==0)dp[a][b][c]=INF;
	else dp[a][b][c]=-INF;
	if(b-s[a]*(1-c)>=0){
		//long long t=solve(a+1,b-s[a]*(1-c),!c);
		if(c==0){
			long long t=-INF;
		//	printf("%d\n",b-s[a]);
			for(int i=0;i<b-s[a];i++){
				//if(i>sum)printf("ng\n");
				t=max(t,solve(a+1,i,1)+1);
			}
			dp[a][b][c]=min(dp[a][b][c],t-r[a]);
		}else{
			long long t=INF;
			for(int i=b+1;i<=sum;i++)t=min(t,solve(a+1,i,0)-1);
			dp[a][b][c]=max(dp[a][b][c],t+r[a]);
		}
	}
	if(b-s[a]*c>=0&&a<n-1){
		long long t=solve(a+1,b-s[a]*c,c);
		if(c==0){
			t=max(t+r[a],1LL);
			dp[a][b][c]=min(dp[a][b][c],t);
		}else{
			t=min(t-r[a],-1LL);
			dp[a][b][c]=max(dp[a][b][c],t);
		}
	}
//	printf("%d %d %d: %lld\n",a,b,c,dp[a][b][c]);
	return dp[a][b][c];
}
int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	n=a;
	for(int i=0;i<a;i++)scanf("%d%d",r+i,s+i);
	sum=0;
	for(int i=0;i<a;i++)sum+=s[i];
	for(int i=sum;i>=0;i--){
		if(b-c>=solve(0,i,0)){
			printf("%d %d\n",i,sum-i);return 0;
		}
	}
	
}
accuracy: 0.5074626865671642
time_ms: 20
memory_kb: 1756
final_rank: 10

code_snippet: #include<cstdio>
#include<algorithm>

using namespace std;

const long long inf=1e12;

int N;
int A,B;
int r[155],s[155];
int Sum;

long long f[155][2][155];

int get(int i,int p,long long x){
	int res=0;
	for(int S=0;S<=Sum;S++){
		if(f[i][p][S]<=x){
			res=S;
		}
	}
	return res;
}

int simulate(int i,int p,long long x){
	if(p==0){
		int res=get(i+1,1,x+r[i])+s[i];
		if(x>0){
			int tmp=get(i+1,0,x-1-r[i]);
			res=max(res,tmp);
		}
		return res;
	}else{
		int res=get(i+1,0,x-r[i]);
		if(x<0){
			int tmp=get(i+1,1,x+1+r[i])+s[i];
			res=min(res,tmp);
		}
		return res;
	}
}

int main(){
	scanf("%d%d%d",&N,&A,&B);
	for(int i=0;i<N;i++){
		scanf("%d%d",r+i,s+i);
		Sum+=s[i];
	}
	for(int i=0;i<N;i++) for(int j=0;j<2;j++){
		for(int k=0;k<=Sum;k++){
			f[i][j][k]=inf;
		}
	}
	f[N-1][0][s[N-1]]=-inf;
	f[N-1][1][0]=-inf;
	for(int i=N-2;i>=0;i--){
		for(int j=0;j<=1;j++){
			for(int s=0;s<=Sum;s++){
				long long lb=-inf/2,ub=inf/2;
				while(ub-lb>1){
					long long x=(ub+lb)/2;
					int val=simulate(i,j,x);
					if(val>=s){
						ub=x;
					}else{
						lb=x;
					}
				}
				if(ub==(-inf/2)+1) f[i][j][s]=-inf;
				else if(ub==(inf/2)) f[i][j][s]=inf;
				else f[i][j][s]=ub;
			}
		}
	}
	int ans=0;
	for(int i=0;i<=Sum;i++){
		if(f[0][0][i]<=A-B){
			ans=i;
		}
	}
	printf("%d %d\n",ans,Sum-ans);
	return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 1400
final_rank: 6