Problem:
Barter
Problem
ナナツ君は、うまか棒を
x
本、ふがしを
y
本持っている。
n
人のお菓子交換人がいる。
    それぞれの交換人
i
は、
ナナツ君のうまか棒
a
i
本と交換人のふがし
b
i
本
ナナツ君のふがし
c
i
本と交換人のブタメソ
d
i
個
のどちらか1つの方法で1回だけ交換してくれる。
この制約の下で、最終的に所持しているブタメソの個数を最大化せよ。
Input
入力は以下の形式で与えられる。
n
x
y
a
1
b
1
c
1
d
1
a
2
b
2
c
2
d
2
...
a
n
b
n
c
n
d
n
1行目に整数
n
が与えられる。
2行目に整数
x
,
y
が空白区切りで与えられる。
3行目から
n
+2行目に整数
a
i
,
b
i
,
c
i
,
d
i
が空白区切りで与えられる。
Constraints
1 ≤
n
≤ 100
0 ≤
x
,
y
≤ 300
1 ≤
a
i
,
b
i
,
c
i
,
d
i
≤ 300
\(\sum_{i=1}^na_i\) ≤ 300, \(\sum_{i=1}^nb_i\) ≤ 300, \(\sum_{i=1}^nc_i\) ≤ 300, \(\sum_{i=1}^nd_i\) ≤ 300
Output
最終的にナナツ君が所持しているブタメソの個数の最大値を1行に出力する
Sample Input 1
3
3 3
3 2 4 1
5 1 2 1
3 1 3 2
Sample Output 1
3
Sample Input 2
3
3 4
3 1 3 1
3 1 4 1
3 1 2 1
Sample Output 2
2
Sample Input 3
4
5 0
5 1 1 1
2 1 2 1
4 1 1 1
3 1 3 1
Sample Output 3
2
Sample Input 4
4
0 0
1 10 1 10
2 5 2 5
3 25 5 6
1 15 2 20
Sample Output 4
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#define pt(x) cout << x << endl;
#define Mid ((l + r) / 2)
#define lson (rt << 1)
#define rson (rt << 1 | 1)
using namespace std;
const int N = 609;
int t[2][N * 2][N * 2], n, x, y;
int ans = 0;
signed main()
{
	memset(t, 0xcf, sizeof(t));
	scanf("%d%d%d", &n, &x, &y);
	t[0][x + N][y + N] = 0;
	for(int i = 1; i <= n; i++) {
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		int (&f)[N * 2][N * 2] = t[i & 1];
		int (&g)[N * 2][N * 2] = t[(i - 1) & 1];
//		memset(f, 0xcf, sizeof(f));
		memcpy(f, g, sizeof(g));
		for(int j = 0; j < 2 * N; j++) {
			for(int k = 0; k < 2 * N; k++) if(g[j][k] != 0xcfcfcfcf){
				if(a <= j && k + b < 2 * N) {
					f[j - a][k + b] = max(f[j - a][k + b], g[j][k]);
				}
				if(c <= k) {
					f[j][k - c] = max(f[j][k - c], g[j][k] + d);
				}
			}
		}
		if(i == n) {
			for(int j = N; j < 2 * N; j++) 
				for(int k = N; k < 2 * N; k++)
					ans = max(ans, f[j][k]);
		}
		
	}
	printf("%d\n", ans);
	return 0;
}
/*
11
7 50
20 26 4 1
21 15 15 17
12 15 12 22
26 25 11 9
11 24 10 3
25 9 14 18
7 10 18 11
5 21 11 20
14 12 18 1
9 26 11 8
4 1 1 18

*/
accuracy: 1
time_ms: 180
memory_kb: 14876
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define pt(x) cout << x << endl;
#define Mid ((l + r) / 2)
#define lson (rt << 1)
#define rson (rt << 1 | 1)
using namespace std;
const int N = 609;
int t[2][N * 2][N * 2], n, x, y;
int ans = 0;
signed main()
{
	memset(t, 0xcf, sizeof(t));
	scanf("%d%d%d", &n, &x, &y);
	t[0][x + N][y + N] = 0;
	for(int i = 1; i <= n; i++) {
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		int (&f)[N * 2][N * 2] = t[i & 1];
		int (&g)[N * 2][N * 2] = t[(i - 1) & 1];
		memset(f, 0xcf, sizeof(f));
		for(int j = 0; j < 2 * N; j++) {
			for(int k = 0; k < 2 * N; k++) if(g[j][k] != 0xcfcfcfcf){
				if(a <= j && k + b < 2 * N) {
					f[j - a][k + b] = max(f[j - a][k + b], g[j][k]);
				}
				if(c <= k) {
					f[j][k - c] = max(f[j][k - c], g[j][k] + d);
				}
			}
		}
		for(int j = N; j < 2 * N; j++) 
			for(int k = N; k < 2 * N; k++)
				ans = max(ans, f[j][k]);
	}
	printf("%d\n", ans);
	return 0;
}
accuracy: 0.09523809523809523
time_ms: 10
memory_kb: 14808
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define pt(x) cout << x << endl;
#define Mid ((l + r) / 2)
#define lson (rt << 1)
#define rson (rt << 1 | 1)
using namespace std;
const int N = 609;
int t[2][N * 2][N * 2], n, x, y;
int ans = 0;
signed main()
{
	memset(t, 0xcf, sizeof(t));
	scanf("%d%d%d", &n, &x, &y);
	t[0][x + N][y + N] = 0;
	for(int i = 1; i <= n; i++) {
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		int (&f)[N * 2][N * 2] = t[i & 1];
		int (&g)[N * 2][N * 2] = t[(i - 1) & 1];
		memset(f, 0xcf, sizeof(f));
		for(int j = 0; j < 2 * N; j++) {
			for(int k = 0; k < 2 * N; k++) if(g[j][k] != 0xcfcfcfcf){
				if(a <= j && k + b < 2 * N) {
					f[j - a][k + b] = max(f[j - a][k + b], g[j][k]);
					if(j - a >= N && k + b >= N) ans = max(ans, f[j - a][k + b]);
				}
				if(c <= k) {
					f[j][k - c] = max(f[j][k - c], g[j][k] + d);
					if(k - c >= N && j >= N) ans = max(ans, f[j][k - c]);
				}
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}
accuracy: 0.09523809523809523
time_ms: 10
memory_kb: 14808
final_rank: 14

code_snippet: // ふがしは初期300+交換+300で最大600本持つ可能性があり、
// これを素直に実装すれば301(うまか[0,300])*901(ふがし[-300,600])
// ところが、sum(c_i)が300以下なので、ふがしを301本以上持つのは無意味
// よってふがしを301以上持つ状態は無いものとして
// 301(うまか[0,300])*601(ふがし[-300,300])で実装してよい
#include<iostream>
#include<vector>
using namespace std;

vector<vector<int>> dp(301, vector<int>(601, -10000));

int main(){
    int n, x, y;
    scanf("%d", &n);
    scanf("%d %d", &x, &y);
    dp[x][300+y] = 0;
    for(int i = 0; i < n; i++){
        vector<vector<int>> ndp = dp;
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        for(int j = 0; j <= 300; j++){
            for(int k = 0; k <= 600; k++){
                if(dp[j][k] < 0)    continue;
                if(j >= a && k+b <= 600)  ndp[j-a][k+b] = max(ndp[j-a][k+b], dp[j][k]);
                if(k >= c)  ndp[j][k-c] = max(ndp[j][k-c], dp[j][k]+d);
            }
        }
        dp = ndp;
    }
    int ans = 0;
    for(int i = 0; i <= 300; i++){
        for(int j = 300; j <= 600; j++){
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4388
final_rank: 1

code_snippet: #include<iostream>
#include<vector>
using namespace std;

vector<vector<int>> dp(301, vector<int>(901, -10000));

int main(){
    int n, x, y;
    scanf("%d", &n);
    scanf("%d %d", &x, &y);
    dp[x][300+y] = 0;
    for(int i = 0; i < n; i++){
        vector<vector<int>> ndp = dp;
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        for(int j = 0; j <= 300; j++){
            for(int k = 0; k <= 900; k++){
                if(dp[j][k] < 0)    continue;
                if(j >= a)  ndp[j-a][k+b] = max(ndp[j-a][k+b], dp[j][k]);
                if(k >= c)  ndp[j][k-c] = max(ndp[j][k-c], dp[j][k]+d);
            }
        }
        dp = ndp;
    }
    int ans = 0;
    for(int i = 0; i <= 300; i++){
        for(int j = 300; j <= 900; j++){
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5008
final_rank: 5

code_snippet: // コピー含め8.1e7で間に合うはず
#include<iostream>
#include<vector>
using namespace std;

vector<vector<int>> dp(901, vector<int>(901, -10000));

int main(){
    int n, x, y;
    scanf("%d", &n);
    scanf("%d %d", &x, &y);
    dp[300+x][300+y] = 0;
    for(int i = 0; i < n; i++){
        vector<vector<int>> ndp = dp;
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        for(int j = 0; j <= 900; j++){
            for(int k = 0; k <= 900; k++){
                if(dp[j][k] < 0)    continue;
                if(j >= a)  ndp[j-a][k+b] = max(ndp[j-a][k+b], dp[j][k]);
                if(k >= c)  ndp[j][k-c] = max(ndp[j][k-c], dp[j][k]+d);
            }
        }
        dp = ndp;
    }
    int ans = 0;
    for(int i = 300; i <= 900; i++){
        for(int j = 300; j <= 900; j++){
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 9252
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
typedef long long int ll;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T>
bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}
template<typename T>
bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}
// clang-format on

const int BS = 310;
int dp[110][BS][BS * 3];

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, x, y;
    cin >> n >> x >> y;
    vector<int> a(n), b(n), c(n), d(n);
    REP(i, 0, n) {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    REP(i, 0, 110) {
        REP(j, 0, BS) {
            REP(k, 0, BS * 3) {
                dp[i][j][k] = -1;
            }
        }
    }

    dp[0][x][y + BS] = 0;

    REP(i, 0, n) {
        REP(j, 0, BS) {
            REP(k, 0, BS * 3) {
                if (dp[i][j][k] == -1) continue;
                if (j >= a[i] && k + b[i] < BS * 3) {
                    chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]);
                }
                if (k >= c[i]) {
                    chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]);
                }
                chmax(dp[i + 1][j][k], dp[i][j][k]);
            }
        }
    }
    int ans = 0;
    REP(i, 0, BS) {
        REP(j, BS, BS * 3) {
            chmax(ans, dp[n][i][j]);
        }
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 127092
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
typedef long long int ll;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T>
bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}
template<typename T>
bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}
// clang-format on

const int BS = 310;
int dp[110][BS][BS * 3];

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, x, y;
    cin >> n >> x >> y;
    vector<int> a(n), b(n), c(n), d(n);
    REP(i, 0, n) {
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    REP(i, 0, 110) {
        REP(j, 0, BS) {
            REP(k, 0, BS * 3) {
                dp[i][j][k] = -1;
            }
        }
    }

    dp[0][x][y + BS] = 0;

    REP(i, 0, n) {
        REP(j, 0, BS) {
            REP(k, 0, BS * 3) {
                if (dp[i][j][k] == -1) continue;
                if (j >= a[i]) {
                    chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]);
                }
                chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]);
            }
        }
    }
    int ans = 0;
    REP(i, 0, BS) {
        REP(j, BS, BS * 3) {
            chmax(ans, dp[n][i][j]);
        }
    }
    cout << ans << endl;

    return 0;
}
accuracy: 0.09523809523809523
time_ms: 30
memory_kb: 126956
final_rank: 17

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 1580.cc: Barter
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_X = 300;
const int MAX_Y = 300;

/* typedef */

/* global variables */

int as[MAX_N], bs[MAX_N], cs[MAX_N], ds[MAX_N];
int dp[2][MAX_X + 1][MAX_X + MAX_Y + 1];

/* subroutines */

inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n, x, y;
  scanf("%d%d%d", &n, &x, &y);

  int bsum = 0, csum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", as + i, bs + i, cs + i, ds + i);
    bsum += bs[i], csum += cs[i];
  }
  //printf("bsum=%d, csum=%d\n", bsum, csum);

  memset(dp, -1, sizeof(dp));
  int z = (bsum > y) ? bsum - y : 0;
  int maxy = z + y + bsum;

  dp[0][x][z + y] = 0;
  int cur = 0, nxt = 1;

  for (int i = 0; i < n; i++) {
    memcpy(dp[nxt], dp[cur], sizeof(dp[cur]));
    for (int j = 0; j <= x; j++)
      for (int k = 0; k <= maxy; k++)
	if (dp[cur][j][k] >= 0) {
	  if (j >= as[i])
	    setmax(dp[nxt][j - as[i]][k + bs[i]], dp[cur][j][k]);
	  if (k >= cs[i])
	    setmax(dp[nxt][j][k - cs[i]], dp[cur][j][k] + ds[i]);
	}

    cur ^= 1, nxt ^= 1;
  }

  int maxd = 0;
  for (int j = 0; j <= x; j++)
    for (int k = z; k <= maxy; k++)
      setmax(maxd, dp[cur][j][k]);

  printf("%d\n", maxd);
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4704
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int INF = 1234567;

const int N = 303;
const int M = 634;
int dp[N][M];
int nx[N][M];

int main(){
    int n,x,y;
    cin >>n >>x >>y;

    rep(j,N)rep(k,M) dp[j][k] = -INF;
    dp[x][N+y] = 0;

    rep(i,n){
        int a,b,c,d;
        cin >>a >>b >>c >>d;

        rep(j,N)rep(k,M) nx[j][k] = dp[j][k];
        rep(j,N)rep(k,M){
            if(j-a>=0 && k+b<M) nx[j-a][k+b] = max(nx[j-a][k+b], dp[j][k]);
            if(k-c>=0) nx[j][k-c] = max(nx[j][k-c], dp[j][k]+d);
        }

        rep(j,N)rep(k,M) dp[j][k] = nx[j][k];
    }

    int ans = 0;
    rep(i,N)for(int j=N; j<M; ++j) ans = max(ans, dp[i][j]);
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4624
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int dp[2][1200][1200];
int main(){
  memset(dp,-1,sizeof(dp));
  int x,y,n;
  cin>>n>>x>>y;
  dp[0][x+600][y+600]=0;
  r(i,n){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    r(A,1200)r(B,1200){
      if(~dp[i%2][A][B]){
        dp[(i+1)%2][A][B]=max(dp[(i+1)%2][A][B],dp[i%2][A][B]);
        dp[(i+1)%2][A-a][B+b]=max(dp[(i+1)%2][A-a][B+b],dp[i%2][A][B]);
        dp[(i+1)%2][A][B-c]=max(dp[(i+1)%2][A][B-c],dp[i%2][A][B]+d);
      }
    }
    r(j,1200)r(k,1200)dp[i%2][j][k]=-1;
  }
  int ans=0;
  r(i,1200)r(j,1200){
    if(i>=600&&j>=600){
      ans=max(ans,dp[n%2][i][j]);
    }
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 140
memory_kb: 14356
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int dp[2][1200][1200];
int main(){
  memset(dp,-1,sizeof(dp));
  int x,y,n;
  cin>>n>>x>>y;
  dp[0][x+600][y+600]=0;
  r(i,n){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    r(A,1200)r(B,1200){
      if(~dp[i%2][A][B]){
        dp[(i+1)%2][A-a][B+b]=max(dp[(i+1)%2][A-a][B+b],dp[i%2][A][B]);
        dp[(i+1)%2][A][B-c]=max(dp[(i+1)%2][A][B-c],dp[i%2][A][B]+d);
      }
    }
    r(j,1200)r(k,1200)dp[i%2][j][k]=-1;
  }
  int ans=0;
  r(i,1200)r(j,1200){
    if(i>=600&&j>=600){
      ans=max(ans,dp[n%2][i][j]);
    }
  }
  cout<<ans<<endl;
}
accuracy: 0.09523809523809523
time_ms: 30
memory_kb: 14356
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int x,y;
int dp[101][301][1201];
int a[101],b[101],c[101],d[110];

int main(void){
	scanf("%d",&n);
	scanf("%d%d",&x,&y);
	for(int i=0;i<n;i++){
		scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
	}
	memset(dp,-1,sizeof(dp));
	dp[0][x][y+300]=0;
	int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<=300;j++){
			for(int k=0;k<=900;k++){
				if(dp[i][j][k]>=0){
					dp[i+1][j][k]=max(dp[i][j][k],dp[i+1][j][k]);
					if(j>=a[i]){
						dp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);
						if(k+b[i]>=300)ans=max(ans,dp[i+1][j-a[i]][k+b[i]]);
					}
					if(k>=c[i]){
						dp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);
						if(k-c[i]>=300)ans=max(ans,dp[i+1][j][k-c[i]]);
					}
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 145840
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int x,y;
int dp[101][301][901];
int a[101],b[101],c[101],d[110];

int main(void){
	scanf("%d",&n);
	scanf("%d%d",&x,&y);
	for(int i=0;i<n;i++){
		scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
	}
	memset(dp,-1,sizeof(dp));
	dp[0][x][y+300]=0;
	int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<=300;j++){
			for(int k=0;k<=900;k++){
				if(dp[i][j][k]>=0){
					dp[i+1][j][k]=max(dp[i][j][k],dp[i+1][j][k]);
					if(j>=a[i]){
						dp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);
					}
					if(k>=c[i]){
						dp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);
						if(k-c[i]>=300)ans=max(ans,dp[i+1][j][k-c[i]]);
					}
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}
accuracy: 0.047619047619047616
time_ms: 30
memory_kb: 110172
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp make_pair
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define chmax(a,b) a = max(a,b)

int dp[2][305][1000];
int a[105], b[105], c[105], d[105];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);

  int n,x,y;
  cin>>n>>x>>y;
  rep(i,n) cin>>a[i]>>b[i]>>c[i]>>d[i];

  auto cur = dp[0];
  auto nxt = dp[1];

  const int INF = 12345678;
  fill(cur[0], cur[303], -INF);

  cur[x][y+300] = 0;
  rep(i,n){
    fill(nxt[0], nxt[303], -INF);
    rep(j,301) for(int k=-300; k<=600; k++) if(cur[j][k+300]>=0){
      int tmp = cur[j][k+300];
      chmax(nxt[j][k+300], tmp);
      if(j>=a[i]) chmax(nxt[j-a[i]][k+300+b[i]], tmp);
      chmax(nxt[j][k+300-c[i]], tmp + d[i]);
    }
    swap(cur, nxt);
  }

  int ans = 0;
  rep(i,301) rep(j,601) ans = max(ans, cur[i][j+300]);

  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5596
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int x,y;
int dp[101][301][601];
int a[101],b[101],c[101],d[110];

int main(void){
	scanf("%d",&n);
	scanf("%d%d",&x,&y);
	for(int i=0;i<n;i++){
		scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
	}
	memset(dp,-1,sizeof(dp));
	dp[0][x][y]=0;
	int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<=300;j++){
			for(int k=0;k<=600;k++){
				if(dp[i][j][k]>=0){
					dp[i+1][j][k]=max(dp[i][j][k],dp[i+1][j][k]);
					if(j>=a[i]){
						dp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);
					}
					if(k>=c[i]){
						dp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);
						ans=max(ans,dp[i+1][j][k-c[i]]);
					}
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}
accuracy: 0.047619047619047616
time_ms: 20
memory_kb: 74588
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int x,y;
int dp[101][601][601];
int a[101],b[101],c[101],d[110];

int main(void){
	scanf("%d",&n);
	scanf("%d%d",&x,&y);
	for(int i=0;i<n;i++){
		scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
	}
	memset(dp,-1,sizeof(dp));
	dp[0][x][y]=0;
	int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<=600;j++){
			for(int k=0;k<=600;k++){
				if(dp[i][j][k]>=0){
					if(j>=a[i]){
						dp[i+1][j-a[i]][k+b[i]]=max(dp[i][j][k],dp[i+1][j-a[i]][k+b[i]]);
					}
					if(k>=c[i]){
						dp[i+1][j][k-c[i]]=max(dp[i][j][k]+d[i],dp[i+1][j][k-c[i]]);
						ans=max(ans,dp[i+1][j][k-c[i]]);
					}
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}
accuracy: 0.047619047619047616
time_ms: 40
memory_kb: 145684
final_rank: 20

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int a[100],b[100],c[100],d[100];
int dp[101][901][901];


int main(){

	int N;
	scanf("%d",&N);

	int first_a,first_b;
	scanf("%d %d",&first_a,&first_b);

	for(int i = 0; i < N; i++){
		scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
	}

	for(int i = 0; i <= N; i++){
		for(int k = 0; k <= 900; k++){
			for(int p = 0; p <= 900; p++)dp[i][k][p] = -1;
		}
	}

	int BASE = 300;

	dp[0][BASE+first_a][BASE+first_b] = 0;

	for(int i = 0; i < N; i++){
		for(int k = 0; k <= 900; k++){
			for(int p = 0; p <= 900; p++){
				if(dp[i][k][p] == -1)continue;

				dp[i+1][k][p] = max(dp[i+1][k][p],dp[i][k][p]);

				if(k >= a[i]){
					dp[i+1][k-a[i]][p+b[i]] = max(dp[i+1][k-a[i]][p+b[i]],dp[i][k][p]);
				}

				if(p >= c[i]){
					dp[i+1][k][p-c[i]] = max(dp[i+1][k][p-c[i]],dp[i][k][p]+d[i]);
				}
			}
		}
	}

	int ans = 0;
	for(int k = BASE; k <= 900; k++){
		for(int p = BASE; p <= 900; p++)ans = max(ans,dp[N][k][p]);
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 323468
final_rank: 12

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

int dp[301][301][901];
struct exc {
	int a;
	int b;
	int c;
	int d;
};
int main() { 
	int N; cin >> N;
	int X, Y; cin >> X >> Y;
	for (int i = 0; i < 301; ++i) {
		for (int j = 0; j < 301; ++j) {
			for (int k = 0; k < 901; ++k) {
				dp[i][j][k] = -1e9;
			}
		}
	}
	dp[0][X][Y + 300] = 0;
	vector<exc>es;
	for (int i = 0; i < N; ++i) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		es.push_back(exc{ a,b,c,d });
	}
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < 301; ++j) {
			for (int k = 0; k < 901; ++k) {
				exc ex(es[i]);
				dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
				{
					int nj = j - ex.a;
					int nk = k+ex.b;
					if (nj < 0) {

					}
					else {
						if (nk > 900)nk = 900;
						dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i][j][k]);
					}
				}
				{
					int nk = k - ex.c;
					if (nk < 0) {

					}
					else {
						dp[i + 1][j][nk] = max(dp[i + 1][j][nk], dp[i][j][k] + ex.d);
					}
				}
			}
		}
	}
	int ans = 0;
	for (int j = 0; j <= 300; ++j) {
		for (int k = 300; k <= 900; ++k) {
			ans = max(ans, dp[N][j][k]);
		}
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 322028
final_rank: 13

code_snippet: #include <iostream>
#include <vector>

constexpr int MAXA   = 350;
constexpr int MAXB   = 700;
constexpr int OFFSET = 350;

void chmax(int &a, int b)
{
    a = (a < b ? b : a);
}

int main()
{
    int n, x, y;
    std::cin >> n >> x >> y;
    
    std::vector<int> a(n), b(n), c(n), d(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    
    std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1));
    dp[x][y + OFFSET] = 0;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < MAXA; j++) {
            for (int k = 0; k < MAXB; k++) {
                if (dp[j][k] == -1) { continue; }

                if (j - a[i] >= 0 && k + b[i] < MAXB) {
                    chmax(dp[j - a[i]][k + b[i]], dp[j][k]);
                }

                if (k - c[i] >= 0) {
                    chmax(dp[j][k - c[i]], dp[j][k] + d[i]);
                }
            }
        }
    }

    int ret = 0;
    for (int i = 0; i < MAXA; i++) {
        for (int j = OFFSET; j < MAXB; j++) {
            chmax(ret, dp[i][j]);
        }
    }
    
    std::cout << ret << std::endl;
    
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3948
final_rank: 2