Problem: Example

Input

2 5 4
5 7
4 8


Output

8 7

Solution (in CPP):

Solution:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const ll INF = 1ll<<40;
const double EPS = 1e-8;
const int MOD = 1000000007;


int n, A, B;
ll r[200], s[200];
ll sum[200];

int solve(int player, int i, ll nut);

int dfs(int player, int i, ll nut){
	if(i == n) return 0;
	int res = s[i] + sum[n] - sum[i+1] - solve(!player, i+1, -(nut + r[i]));
	if(nut > 0) res = max(res, solve(player, i+1, (nut - r[i] - 1)));
	return res;
}

map<ll, int> memo[2][200];

void fillTable(int player, int i){
	map<ll, int> &tar = memo[player][i];
	int low = tar[-INF] = dfs(player, i, -INF/2);
	int hi = tar[INF] = dfs(player, i, INF/2)+1;
	for(int j=low+1;j<hi;j++){
		ll l=-INF, r = INF;
		while(l<r-1){
			ll med = (l+r)/2;
			if(dfs(player, i, med) < j) l = med;
			else r = med;
		}
		j = tar[r] = dfs(player, i, r);
	}
}

int solve(int player, int i, ll nut){
	if(i == n) return 0;
	if(memo[player][i].empty()) fillTable(player, i);
	auto it = (memo[player][i].upper_bound(nut));
	return (--it)->second;
}

int main(){
	ios::sync_with_stdio(false);
	cin >> n >> A >> B;
	REP(i, n){
		cin >> r[i] >> s[i];
		sum[i+1] = sum[i] + s[i];
	}
	int res = solve(0, 0, A-B);
	cout << res << " " << sum[n] - res << endl;
	return 0;
}