Problem:
Anipero 2012
D: アニペロ2012
アニペロサマーライブ，通称アニペロは，さまざまなアニメソングアーティストたちが集結する，日本国内最大のアニメソングライブイベントである．アニソンが大好きな2D君は，昨年に引き続き，今年もアニペロに行くことにした．
彼は，アニペロを楽しむために，すでにサイリウムを
m
本購入している．サイリウムとは，折ると化学反応で光るスティックである．リズムに合わせてサイリウムを振ることで，ライブを盛り上げることができ，自分の満足度も上昇する．今回，彼が購入した全てのサイリウムは，次のような性質を持つ．
サイリウムは，折ってから時間が経過するにつれ暗くなっていき，10分で光を失う．
折ってから5分の間は，非常に明るい（以下，「レベル2」と呼ぶ）．
折ってから5分経過した後は，少し暗くなる（以下，「レベル1」と呼ぶ）．
振っても振らなくても，光を失うスピードは変わらない．
2D君は，限られたサイリウムを曲によってうまく使い分けることで，今年のアニペロでどれだけの満足度を得られそうか，以下の問題を考えることにした．
彼は，ライブ中に流れるであろう曲を
n
曲予想している．
各曲の長さは，全て5分である．
n
曲は，連続で流れ続け，曲と曲の間は，0分と考えてよい．
各曲には，以下の3つのパラメータが与えられる．
ある曲に対して，レベル2を1本だけ振ったときに増える満足度
ある曲に対して，レベル1を1本だけ振ったときに増える満足度
ある曲において，1本も振らないときに増える満足度
サイリウムを振ったからといって，満足度が増えるとは限らない．
振ったら逆に会場の雰囲気を乱して，満足度が減る場合もある．
振らない場合も，同様のことが言える．
サイリウムは，以下のルールに従って使用しなければならない．
曲が始まる時点でのみ折ることができ，一度に何本でも折ることができる．サイリウムを折るのにかかる時間は無視してよい．
1曲で最大8本同時に振ることができる．
複数のサイリウムを振る際，以下の式により加算する満足度を計算する．
(レベル1の本数)×(レベル1の1本あたりの満足度)＋(レベル2の本数)×(レベル2の1本あたりの満足度)
1本もサイリウムを振らない場合は，1本も振らない場合の満足度だけを加算する．
一度振ると決めたサイリウムは，1曲が終了するまで持ち替えることはできない．
サイリウムは，振らずに置いておくことができる．また，サイリウムを全て使いきる必要はない．
2D君は，ライブの曲予想までは済ませたが，それだけで疲れてしまって，問題を解く気になれなかった．
あなたの仕事は，彼の代わりに，今年のライブで得られそうな最大満足度を求めるプログラムを書くことである．
Input
ライブの予想曲リストに対する満足度情報が入力される．
1行目に，ライブで歌われる曲数
n
(1 <=
n
<= 50)と，ライブ開始時に2D君が持っている新品のサイリウムの数m (0 <=
m
<= 50)がスペース区切りで入力される．
続く
n
行では，1行ずつ1曲の情報が入力される．
i
番目(1 <=
i
<=
n
)の曲情報は，
曲
i
に対して，レベル2のサイリウムを1本振ったときの満足度
a
i
曲
i
に対して，レベル1のサイリウムを1本振ったときの満足度
b
i
曲
i
において，サイリウムを1本も振らないときの満足度
c
i
がスペース区切りで入力される(-100 <=
a
i
,
b
i
,
c
i
<= 100)．
Output
ライブ終了時の2D君の最大満足度の予想を1行に出力せよ．
最大満足度がマイナスになることもあるので，注意すること．
行の最後には，改行を出力すること．
Sample Input 1
1 5
2 3 8
Sample Output 1
10
Sample Input 2
2 10
2 2 20
2 3 -10
Sample Output 2
44
Sample Input 3
3 10
5 1 9
-3 2 1
1 11 0
Sample Output 3
102

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>
#define VAL 55
using namespace std;
using lli = long long int;
lli dp[VAL][VAL][VAL][VAL]; // 曲，サイリウム数，状態1，状態2

int main(){
  int n, m;
  std::cin >> n >> m;
  for (int i = 0; i < VAL*VAL*VAL*VAL; i++) {
    const int x = VAL*VAL*VAL, y = VAL*VAL, z = VAL;
    dp[i/x][(i/y)%z][(i/z)%z][i%z] = -1e17;
  }
  vector<vector<lli> > music(n, vector<lli>(3));
  for (int i = 0; i < n; i++) {
    int x, y, z;
    std::cin >> x >> y >> z;
    music[i][0] = x;
    music[i][1] = y;
    music[i][2] = z;
  }
  dp[0][m][0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      for (int k = 0; k <= min(8, j); k++) {
        for (int a = 0; a <= 8; a++) {//状態1
          for (int b = 0; b <= 8; b++) {//状態2
            if(dp[i][j][a][b] == -1e17)continue;
            lli cost = -1e17;
            if(min(k, 8)*music[i][0] + min(a, 8 - min(k, 8))*music[i][1] > 0)
              cost = max(cost, min(k, 8)*music[i][0] + min(a, 8 - min(k, 8))*music[i][1]);
            if(min(k, 8 - min(a, 8))*music[i][0] + min(a, 8)*music[i][1] > 0)
              cost = max(cost, min(k, 8 - min(a, 8))*music[i][0] + min(a, 8)*music[i][1]);
            if(k > 0)cost = max(cost, music[i][0]);
            if(a > 0)cost = max(cost, music[i][1]);
            cost = max(cost, music[i][2]);
            dp[i + 1][j - k][k][a] =
              max(dp[i + 1][j - k][k][a], dp[i][j][a][b] + cost);
          }
        }
      }
    }
  }
  lli ans = -1e17;
  for (int i = 0; i <= 50; i++) {
    for (int j = 0; j <= 50; j++) {
      for (int k = 0; k <= 50; k++) {
        ans = max(ans, dp[n][i][j][k]);        
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 74640
final_rank: 18

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)
void maxUpdate(int& a, int b) { a = max(a, b); }

int n, m, a[55], b[55], c[55], dp[55][55][55];

int getMaxsatis(int i, int L1, int L2) {
	int res = c[i];
	for_(x,0,L1+1) for_(y,0,L2+1) {
		if (x + y == 0 || x + y > 8) continue;
		maxUpdate(res, a[i] * y + b[i] * x);
	}
	return res;
}

int main() {
	cin >> n >> m;
	for (int i=0; i<n; ++i) cin >> a[i] >> b[i] >> c[i];
	
	for_(i,0,n+1) for_(j,0,m+1) for_(k,0,m+1) dp[i][j][k]= -(1L << 30);
	
	dp[0][0][0] = 0;
	for_(i,0,n) for_(j,0,m+1) for_(k,0,m+1) {
		if (dp[i][j][k] == -(1L << 30)) continue;
		for_(x,0,m-j-k+1) maxUpdate(dp[i+1][j+k][x], dp[i][j][k] + getMaxsatis(i, k, x));
	}
	
	int ans = -(1L << 30);
	for_(j,0,m+1) for_(k,0,m+1) maxUpdate(ans, dp[n][j][k]);
	cout << ans << endl;
}
accuracy: 1
time_ms: 160
memory_kb: 3632
final_rank: 11

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

vector<vector<vector<int>>>dp;
int main() {
	int N, M; cin >> N >> M;
	dp = vector<vector<vector<int>>>(N+1, vector<vector<int>>(M + 1, vector<int>(M+1, -1e9)));
	dp[0][M][0] = 0;
	vector<tuple<int, int, int>>musics;
	for (int i = 0; i < N; ++i) {
		int a, b, c; cin >> a >> b >> c;
		musics.emplace_back(a,b, c);
	}
	for (int music = 0; music < N; ++music) {
		for (int two = 0; two <= M; ++two) {
			for (int one = 0; one <= M; ++one) {
				for (int usetwo = 0; usetwo <= min(8,two); ++usetwo) {
					for (int useone = 0; useone <= min(8-usetwo, one); ++useone) {
						for (int destwo = 0; destwo <= min(8,two - usetwo); ++destwo) {
							int plus = 0;
							if (usetwo == 0 && useone == 0)plus = get<2>(musics[music]);
							else {
								plus += get<1>(musics[music])*useone;
								plus += get<0>(musics[music])*usetwo;
							}
							dp[music + 1][two - usetwo-destwo][usetwo+destwo] = max(dp[music + 1][two - usetwo-destwo][usetwo+destwo], dp[music][two][one] + plus);

						}
					}
				}
			}
		}
	}
	int ans = -1e9;
	for (int i = 0; i <= M; ++i) {
		for (int j = 0; j <= M; ++j) {
			ans = max(ans, dp[N][i][j]);
		}
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3316
final_rank: 9

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>
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;

struct Info{
	int value2,value1,value0;
};

struct Data{
	Data(int arg_music,int arg_new_num,int arg_level_1_num){
		music = arg_music;
		new_num = arg_new_num;
		level_1_num = arg_level_1_num;
	}
	int music,new_num,level_1_num;
};

int dp[51][51][51];
int next_dp[51][51][51];

int main(){

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

	Info info[N+1];

	for(int i = 1; i <= N; i++){
		scanf("%d %d %d",&info[i].value2,&info[i].value1,&info[i].value0);
	}

	for(int music = 0; music <= N; music++){
		for(int new_num = 0; new_num <= M; new_num++){
			for(int level_1_num = 0; level_1_num <= M; level_1_num++){
				dp[music][new_num][level_1_num] = -BIG_NUM;
				next_dp[music][new_num][level_1_num] = -BIG_NUM;
			}
		}
	}

	dp[0][M][0] = 0;
	next_dp[0][M][0] = 0;

	vector<Data> Updated;

	for(int music = 0; music <= N-1; music++){

		for(int new_num = 0; new_num <= M; new_num++){
			for(int level_1_num = 0; level_1_num <= M; level_1_num++){
				if(dp[music][new_num][level_1_num] == -BIG_NUM)continue;

				for(int oru_num = 0; oru_num <= new_num; oru_num++){

					if(next_dp[music+1][new_num-oru_num][oru_num] < dp[music][new_num][level_1_num]+info[music+1].value0){
						next_dp[music+1][new_num-oru_num][oru_num] = dp[music][new_num][level_1_num]+info[music+1].value0;
						Updated.push_back(Data(music+1,new_num-oru_num,oru_num));
					}

					for(int num_2 = 0; num_2 <= oru_num; num_2++){
						for(int num_1 = 0; num_1 <= level_1_num; num_1++){
							if(num_2+num_1 == 0 || num_2+num_1 > 8)continue;

							int satisfy = num_2*info[music+1].value2+num_1*info[music+1].value1;

							if(next_dp[music+1][new_num-oru_num][oru_num] < dp[music][new_num][level_1_num]+satisfy){
								next_dp[music+1][new_num-oru_num][oru_num] = dp[music][new_num][level_1_num]+satisfy;
								Updated.push_back(Data(music+1,new_num-oru_num,oru_num));
							}
						}
					}
				}
			}
		}

		for(int k = 0; k < Updated.size(); k++){
			dp[Updated[k].music][Updated[k].new_num][Updated[k].level_1_num] = next_dp[Updated[k].music][Updated[k].new_num][Updated[k].level_1_num];
		}
		Updated.clear();
	}

	int ans = -BIG_NUM;
	for(int new_num = 0; new_num <= M; new_num++){
		for(int level_1_num = 0; level_1_num <= M; level_1_num++){
			ans = max(ans,dp[N][new_num][level_1_num]);
		}
	}

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

	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 4772
final_rank: 12

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
int n, m, a[55], b[55], c[55], dp[55][55][55][55]; bool vis[55][55][55][55];
int rec(int pos, int rem, int lv2, int lv1) {
	if (pos == n) return 0;
	if (vis[pos][rem][lv2][lv1]) return dp[pos][rem][lv2][lv1];
	int ret = -999999999;
	for (int i = 0; i <= rem; i++) {
		int res = rec(pos + 1, rem - i, i, lv2);
		int cs = c[pos];
		for (int j = 1; j <= 8; j++) {
			for (int k = 0; k <= j; k++) {
				int l = j - k;
				if (k <= lv2 && l <= lv1) {
					cs = max(cs, a[pos] * k + b[pos] * l);
				}
			}
		}
		ret = max(ret, res + cs);
	}
	vis[pos][rem][lv2][lv1] = true;
	return dp[pos][rem][lv2][lv1] = ret;
}
int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i];
	int ret = -999999999;
	for (int i = 0; i <= m; i++) ret = max(ret, rec(0, m - i, i, 0));
	cout << ret << endl;
	return 0;
}
accuracy: 1
time_ms: 690
memory_kb: 33192
final_rank: 19

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int n, m, a[100], b[100], c[100];
int dp[60][60][9];
int main() {
	for (int i = 0; i < 60; i++) { for (int j = 0; j < 60; j++) { for (int k = 0; k < 9; k++)dp[i][j][k] = -999999999; } }
	cin >> n >> m; for (int i = 0; i < n; i++)cin >> a[i] >> b[i] >> c[i]; dp[0][m][0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= m; j++) {
			for (int k = 0; k <= 8; k++) {
				if (dp[i][j][k] <= -100000000)continue;
				for (int l = 0; l <= 8; l++) {
					for (int o = 0; o <= k; o++) {
						for (int r = 0; r <= l; r++) {
							if (r + o >= 9)continue;
							int p = a[i] * r + b[i] * o;
							if (r == 0 && o == 0)p = c[i];
							int q = j - l; if (q < 0)continue;
							dp[i + 1][q][l] = max(dp[i + 1][q][l], dp[i][j][k] + p);
						}
					}
				}
			}
		}
	}
	int maxn = -999999999; for (int i = 0; i <= m; i++) { for (int j = 0; j <= 8; j++)maxn = max(maxn, dp[n][i][j]); }
	cout << maxn << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3260
final_rank: 5

code_snippet: #include <iostream>

using namespace std;

#define MAX 55
#define MINF -(1e8)

int N,M,a[MAX],b[MAX],c[MAX];
int memo[MAX][MAX][MAX];

int solve(int n,int l1,int m){
    if(n == N) return 0;
    int &res = memo[n][l1][m];
    if(res != MINF) return res;
    for(int i = 0 ; i <= min(8,m) ; i++){
        for(int j = 0 ; j <= i ; j++){
            for(int k = 0 ; k <= min(l1,8-j) ; k++){
                int cost = (j+k == 0 ? c[n] : a[n]*j+b[n]*k);
                res = max(res,solve(n+1,i,m-i)+cost);    
            }
        }
    }
    return res;
}

int main(){
    cin >> N >> M;
    for(int i = 0 ; i < N ; i++){
        cin >> a[i] >> b[i] >> c[i];
    }
    fill(&memo[0][0][0],&memo[MAX-1][MAX-1][MAX-1],MINF);
    cout << solve(0,0,M) << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3736
final_rank: 6

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)n; i++)

int n, m;
int x[51], y[51], z[51];
int dp1[51][51][51];
int dp2[51][51][51];
const int INF = 1e9;
int cost(int a, int b, int c) {
    if (dp1[a][b][c] != -INF) return dp1[a][b][c];
    int res = -INF;
    rep(i, b + 1) rep(j, c + 1) {
        if (i == 0 && j == 0)
            res = max(res, z[a]);
        else if (i + j <= 8)
            res = max(res, i * x[a] + j * y[a]);
    }
    return dp1[a][b][c] = res;
}
int dfs(int a, int b, int c) {
    if (a == n) return 0;
    if (dp2[a][b][c] != -INF) return dp2[a][b][c];
    // int res = dfs(a + 1, b, 0) + z[a];
    int res = -INF;

    rep(i, b + 1) { res = max(res, dfs(a + 1, b - i, i) + cost(a, i, c)); }

    return dp2[a][b][c] = res;
}
int main() {
    rep(i, 51) rep(j, 51) rep(k, 51) dp1[i][j][k] = -INF;
    rep(i, 51) rep(j, 51) rep(k, 51) dp2[i][j][k] = -INF;
    cin >> n >> m;
    rep(i, n) cin >> x[i] >> y[i] >> z[i];
    cout << dfs(0, m, 0) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4140
final_rank: 7

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)n; i++)

int n, m;
int x[51], y[51], z[51];
int dp1[51][51][51];
int dp2[51][51][51];
const int INF = 1e9;
int cost(int a, int b, int c) {
    if (dp1[a][b][c] != -INF) return dp1[a][b][c];
    int res = -INF;
    rep(i, b + 1) rep(j, c + 1) {
        if (i == 0 && j == 0)
            res = max(res, z[a]);
        else if (i + j <= 8)
            res = max(res, i * x[a] + j * y[a]);
    }
    return dp1[a][b][c] = res;
}
int dfs(int a, int b, int c) {
    if (a == n) return 0;
    if (dp2[a][b][c] != -INF) return dp2[a][b][c];
    // int res = dfs(a + 1, b, 0) + z[a];
    int res = -INF;

    rep(i, b + 1) {
        int s = -INF;
        rep(j, i + 1) {
            rep(k, c + 1) {
                if (k == 0 && j == 0)
                    s = max(z[a], s);
                else if (j + k <= 8)
                    s = max(s, j * x[a] + k * y[a]);
            }
        }
        res = max(res, dfs(a + 1, b - i, i) + s);
    }

    // rep(i, b + 1) { res = max(res, dfs(a + 1, b - i, i) + cost(a, i, c)); }

    return dp2[a][b][c] = res;
}
int main() {
    rep(i, 51) rep(j, 51) rep(k, 51) dp1[i][j][k] = -INF;
    rep(i, 51) rep(j, 51) rep(k, 51) dp2[i][j][k] = -INF;
    cin >> n >> m;
    rep(i, n) cin >> x[i] >> y[i] >> z[i];
    cout << dfs(0, m, 0) << endl;
}
accuracy: 1
time_ms: 140
memory_kb: 4140
final_rank: 10

code_snippet: /*
 * 2420.cc: Anipero 2012
 */

#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 = 50;
const int MAX_M = 50;
const int MAX_S = 8;

const int INF = 1 << 30;

/* typedef */

/* global variables */

int as[MAX_N], bs[MAX_N], cs[MAX_N];
int dp[MAX_N + 1][MAX_M + 1][MAX_S + 1];

/* subroutines */

/* main */

int main() {
  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) cin >> as[i] >> bs[i] >> cs[i];

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      for (int k = 0; k <= MAX_S; k++) dp[i][j][k] = -INF;
  dp[0][m][0] = 0;

  for (int i = 0; i < n; i++)
    for (int j = 0; j <= m; j++)
      for (int k = 0; k <= MAX_S; k++) // Level 1
	if (dp[i][j][k] > -INF) {
	  int max_s = min(j, MAX_S);
	  for (int s = 0; s <= max_s; s++) { // Level 2
	    int maxd = -INF;
	    for (int an = 0; an <= s; an++) {
	      int max_bn = min(k, MAX_S - an);
	      for (int bn = 0; bn <= max_bn; bn++) {
		int d = (an == 0 && bn == 0) ? cs[i] : as[i] * an + bs[i] * bn;
		if (maxd < d) maxd = d;
	      }
	    }
	    maxd += dp[i][j][k];
	    if (dp[i + 1][j - s][s] < maxd) dp[i + 1][j - s][s] = maxd;
	    //printf("i=%d, maxd=%d\n", i, maxd);
	  }
	}
  
  int ans = -INF;
  for (int j = 0; j <= m; j++)
    for (int k = 0; k <= MAX_S; k++)
      if (ans < dp[n][j][k]) ans = dp[n][j][k];
  printf("%d\n", ans);

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1236
final_rank: 1

code_snippet: #include <iostream>

using namespace std;

#define MAX 55
#define MINF -(1e8)

int N,M,a[MAX],b[MAX],c[MAX];
int memo[MAX][MAX][MAX];

int solve(int n,int l1,int m){
    if(n == N) return 0;
    int &res = memo[n][l1][m];
    if(res != MINF) return res;
    for(int i = 0 ; i <= min(8,m) ; i++){
        for(int j = 0 ; j <= i ; j++){
            for(int k = 0 ; k <= min(l1,8-j) ; k++){
                if(j + k == 0){
                    res = max(res,solve(n+1,i,m-i)+c[n]);    
                }else{
                    res = max(res,solve(n+1,i,m-i)+a[n]*j+b[n]*k);
                }
            }
        }
    }
    return res;
}

int main(){
    cin >> N >> M;
    for(int i = 0 ; i < N ; i++){
        cin >> a[i] >> b[i] >> c[i];
    }
    fill(&memo[0][0][0],&memo[MAX-1][MAX-1][MAX-1],MINF);
    cout << solve(0,0,M) << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1812
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int64;
const int INF = 1 << 30;
int main(){
  int n, m, a[50], b[50], c[50], dp[51][51][51];
  cin >> n >> m;
  for(int i = 0; i < n; i++){
    cin >> a[i] >> b[i] >> c[i];
  }
  fill_n( **dp, 51 * 51 * 51, -INF);
  dp[0][m][0] = 0;
  for(int i = 0; i < n; i++){ // O(nm^3*64) = O(4*10^8)
    for(int j = 0; j <= m; j++){
      for(int k = 0; k <= m - j; k++){ // レベル1のサイリウムの数
        if(dp[i][j][k] == -INF) continue;
        for(int l = j; l >= 0; l--){ // レベル2のサイリウムの数

          //はやくするならここだな
          for(int o = 0; o <= k && o <= 8; o++){ // 実際にふる数 レベル1
            for(int p = 0; p <= l && o + p <= 8; p++){ // 実際にふる数 レベル2
              if(o + p == 0){
                dp[i + 1][j - l][l] = max( dp[i + 1][j - l][l], dp[i][j][k] + c[i]); 
              } else {
                dp[i + 1][j - l][l] = max( dp[i + 1][j - l][l], dp[i][j][k] + b[i] * o + a[i] * p);
              }
            }
          }
        }
      }
    }
  }
  int ret = -INF;
  for(int i = 0; i <= m; i++){
    for(int j = 0; j <= m; j++){
      ret = max( ret, dp[n][i][j]);
    }
  }
  cout << ret << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 1680
final_rank: 8

code_snippet: #include<iostream>
#include<algorithm>

using namespace std;

int main(){
  int n,m;
  cin>>n>>m;
  int dp[51][51][9];
  fill(dp[0][0],dp[51][0],-(1<<29));
  dp[0][m][0]=0;
  for(int i=0;i<n;i++){
    int a,b,c;
    cin>>a>>b>>c;
    for(int j=0;j<=m;j++){
      for(int k=0;k<=8;k++){
	for(int l=0;l<=8&&l<=j;l++){
	  for(int p=0;p<=l;p++){
	    for(int o=0;o+p<=8&&o<=k;o++){
	      int nr=j-l;
	      dp[i+1][nr][l]=max(dp[i+1][nr][l],dp[i][j][k]+((!p&&!o)?c:a*p+b*o));
	    }
	  }
	}
      }
    }
  }
  cout<<*max_element(dp[n][0],dp[n+1][0])<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 1252
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#define MINF 0xc0c0c0c0
 
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
 
static const double EPS = 1e-8;
 
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

int compute_optimal_swing(int level2_satisfaction,
                          int level1_satisfaction,
                          int none_satisfaction,
                          int level2_remaining,
                          int level1_remaining
){
  int res = none_satisfaction;
  for(int use_level2 = 0; use_level2 <= level2_remaining; use_level2++){
    for(int use_level1 = 0; use_level1 <= level1_remaining; use_level1++){
      if(use_level2 + use_level1 > 8) continue;
      if(use_level2 ==0 &&  use_level1 == 0) continue;
      res = max(level2_satisfaction * use_level2
                + level1_satisfaction * use_level1,res);
    }
  }
  return res;
}

int main(){
  int total_songs;
  int cyalume;
  while(~scanf("%d %d",
               &total_songs,
               &cyalume)){

    int dp[51][51]; //next accumulate
    memset(dp,0xc0,sizeof(dp));
    dp[0][0] = 0;

    int res = MINF;
    for(int song_idx=0;song_idx < total_songs;song_idx++){
      int level2_satisfaction,level1_satisfaction,none_satisfaction;
      scanf("%d %d %d",
            &level2_satisfaction,
            &level1_satisfaction,
            &none_satisfaction);

      int next_dp[51][51];
      memset(next_dp,0xc0,sizeof(next_dp));
      
      for(int accumulate=cyalume;accumulate>=0;accumulate--){
        for(int prev=0;prev<=cyalume;prev++){
          if(prev > accumulate) continue;

          for(int next=0;next<=cyalume;next++){
            if(dp[prev][accumulate - next] == MINF) continue;
            if(accumulate - next < 0) continue;

            next_dp[next][accumulate] 
              = max(dp[prev][accumulate - next]
                    + compute_optimal_swing(level2_satisfaction,
                                            level1_satisfaction,
                                            none_satisfaction,
                                            next,prev),
                    next_dp[next][accumulate]);

            if(song_idx == total_songs - 1){
              res = max(res,next_dp[next][accumulate]);
            }
          }
        }
      }
      
      memcpy(dp,next_dp,sizeof(int)*51*51);
    }

    printf("%d\n",res);
  }
}
accuracy: 1
time_ms: 230
memory_kb: 1228
final_rank: 14

code_snippet: #include<iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int dp[60][60][60];

int main() {
  int n, m;
  cin >> n >> m;
  int a[n], b[n], c[n];
  rep (i, n) cin >> a[i] >> b[i] >> c[i];
  rep (i, 60) rep (j, 60) rep (k, 60) dp[i][j][k] = -1e9;
  dp[0][m][0] = 0;
  rep (i, n) rep (j, m + 1) rep (k, m + 1) rep (l, min(j + 1, 9)) {
    if (l + k <= 8 && l + k > 0) dp[i + 1][j - l][l] = max(dp[i + 1][j - l][l], dp[i][j][k] + a[i] * l + b[i] * k);
    if (l > 0) dp[i + 1][j - l][l] = max(dp[i + 1][j - l][l], dp[i][j][k] + a[i] * l);
    if (k > 0) dp[i + 1][j - l][l] = max(dp[i + 1][j - l][l], dp[i][j][k] + b[i] * k);
    if (l > 1) dp[i + 1][j - l][l] = max(dp[i + 1][j - l][l], dp[i][j][k] + a[i]);
    if (k > 1) dp[i + 1][j - l][l] = max(dp[i + 1][j - l][l], dp[i][j][k] + b[i]);
    dp[i + 1][j - l][l] = max(dp[i + 1][j - l][l], dp[i][j][k] + c[i]);
  }
  int res = -1e9;
  rep (i, m + 1) rep (j, m + 1) res = max(res, dp[n][i][j]);
  cout << res << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 2004
final_rank: 3

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int dp[51][51][51][51]={};
const int MIN=INT_MIN/10;
int dfs(int live,int lv0,int lv1,int lv2,vi &a,vi &b,vi &c){
	if(dp[live][lv0][lv1][lv2]!=MIN){
		return dp[live][lv0][lv1][lv2];
	}
	if(live==a.size()){
		return 0;
	}
	int ret=MIN;
	REP(k,lv2+1){
		REP(i,min(8,k)+1){
			REP(j,8-i+1){
				if(lv1>=j){
					int plus=0;
					if(i==0&&j==0){
						plus=c[live];
					}else{
						plus=a[live]*i+b[live]*j;
					}
					ret=max(ret,dfs(live+1,lv0+lv1,k,lv2-k,a,b,c)+plus);
				}
			}
		}
	}
	return dp[live][lv0][lv1][lv2]=ret;
}
int main(){
	int n,m;
	cin>>n>>m;
	vi a(n),b(n),c(n);
	REP(i,51)REP(j,51)REP(k,51)REP(l,51)dp[i][j][k][l]=MIN;
	REP(i,n){
		cin>>a[i]>>b[i]>>c[i];
	}
	cout<<dfs(0,0,0,m,a,b,c)<<endl;

	//REP(i,51){
	//	int aa=0;
	//	REP(j,51)REP(k,51)REP(l,51){
	//		aa=max(aa,dp[i][j][k][l]);
	//	}
	//	cout<<i<<" "<<aa<<endl;
	//}
}
accuracy: 1
time_ms: 260
memory_kb: 27628
final_rank: 17

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

const long long INF = 0xfffffff;

template<class T> inline void chmax(T &a, T b) { if(b > a) a = b; }

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

	int n, m;
	cin >> n >> m;

	vector<int> a(n), b(n), c(n);
	for(int i = 0; i < n; ++i)
		cin >> a[i] >> b[i] >> c[i];

	vector<vector<long long> > dp(m + 1, vector<long long>(9, -INF));
	dp[m][0] = 0;

	for(int i = 0; i < n; ++i) {
		vector<vector<long long> > tmp(m + 1, vector<long long>(9, -INF));

		for(int j = 0; j <= m; ++j) {
			for(int k = 0; k <= 8; ++k) {
				for(int l = 0; l <= min(j, 8); ++l) {
					chmax(tmp[j - l][l], dp[j][k] + c[i]);

					for(int one = 0; one <= k; ++one)
						for(int two = 0; two <= l; ++two)
							if(1 <= one + two && one + two <= 8)
								chmax(tmp[j - l][l], dp[j][k] + two * a[i] + one * b[i]);
				}
			}
		}

		dp.swap(tmp);
	}
	
	long long ans = 0;
	for(int i = 0; i <= m; ++i)
		for(int j = 0; j <= 8; ++j)
			chmax(ans, dp[i][j]);

	cout << ans << endl;

	return EXIT_SUCCESS;
}
accuracy: 0.9130434782608695
time_ms: 10
memory_kb: 1224
final_rank: 20

code_snippet: #include<stdio.h>
#include<string.h>
int ans[51][51][51][51];//ans[n曲目][折った本数][レベル２の本数][レベル１の本数]=満足度
bool calcOK[51][51][51][51];//同上　計算可能のメモ
int main(){
	int n,m;
	int a,b,c;
	scanf("%d %d",&n,&m);
	memset(ans,0,sizeof(ans));
	memset(calcOK,false,sizeof(calcOK));
	for(int i=0;i<=m;i++){
		calcOK[0][i][i][0]=true;
	}
	
	int lastAns=-100000000;
	for(int i=1;i<=n;i++){
		//iはｎ曲目
		scanf("%d %d %d",&a,&b,&c);
		for(int j=0;j<=m;j++){
			//jは折った数
			for(int L2=0;L2<=j;L2++){
				//前回折ったＬｅｖｅｌ２の数
				for(int L1=0;L1+L2<=j;L1++){
					if(calcOK[i-1][j][L2][L1]==false)continue;
					int mMax;
					for(int a1=0;a1<=8&&a1<=L2;a1++){
						for(int b1=0;b1+a1<=8&&b1<=L1;b1++){
							if(a1==0&&b1==0)mMax=c;
							else if(mMax<a*a1+b*b1)mMax=a*a1+b*b1;
						}
					}
					
					for(int nowL2=0;nowL2+j<=m;nowL2++){
						if(calcOK[i][j+nowL2][nowL2][L2]==false){
							ans[i][j+nowL2][nowL2][L2]=ans[i-1][j][L2][L1]+mMax;
							calcOK[i][j+nowL2][nowL2][L2]=true;
						}
						if(ans[i][j+nowL2][nowL2][L2]<ans[i-1][j][L2][L1]+mMax){
							ans[i][j+nowL2][nowL2][L2]=ans[i-1][j][L2][L1]+mMax;
						}
						if(i==n&&lastAns<ans[i][j+nowL2][nowL2][L2])lastAns=ans[i][j+nowL2][nowL2][L2];
					}
				}
			}
		}
	}
	printf("%d\n",lastAns);
}
accuracy: 1
time_ms: 90
memory_kb: 34068
final_rank: 16

code_snippet: #include<iostream>
#include<algorithm>
#define REP(i,a) for(int i=0;i<a;i++)
using namespace std;

int n,m;
int a[60],b[60],c[60];
int dp[60][60][60];

int main(){
  cin >> n >> m;
  REP(i,n)cin >> a[i] >> b[i] >> c[i];

  REP(i,n+1)REP(j,m+1)REP(k,m+1)dp[i][j][k] = -100000000;

  dp[0][m][0] = 0;

  REP(i,n){
    REP(j,m+1)REP(k,m+1){
      REP(z,j+1){
	int s = c[i];
	for(int x=1;x<=8;x++)
	  for(int y=0;y<=x;y++){
	    if(z<y || k<x-y)continue;
	    s = max(s, a[i]*y + b[i]*(x-y));
	  }
	dp[i+1][j-z][z] = max(dp[i+1][j-z][z],dp[i][j][k] + s);
      }
    }
  }

  int ans = -100000000;
  REP(j,m+1)REP(k,m+1)ans = max(ans,dp[n][j][k]);
  cout << ans << endl;
}
accuracy: 1
time_ms: 200
memory_kb: 1880
final_rank: 13

code_snippet: #include <stdio.h>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF (1<<28)

int n, m, a[64], b[64], c[64];
int dp[64][64][64];

int f(int k, int x, int y) {
    int ans = c[k];
    rep (i, x+1) rep (j, y+1) if (1 <= i+j && i+j <= 8) {
        ans = max(ans, a[k]*i + b[k]*j);
    }
    return ans;
}

int main() {
    scanf("%d%d", &n, &m);
    rep (i, n) scanf("%d%d%d", a+i, b+i, c+i);
    rep (i, 64) rep (j, 64) rep (k, 64) dp[i][j][k] = -INF;
    dp[0][0][0] = 0;
    rep (k, n) rep (x, m+1) rep (i, x+1) if (dp[k][x][i] > -INF) {
        rep (j, m-x+1) {
            dp[k+1][x+j][j] = max(dp[k+1][x+j][j], dp[k][x][i]+f(k, j, i));
        }
    }
    int ans = -INF;
    rep (x, m+1) rep (i, 64) ans = max(ans, dp[n][x][i]);
    printf("%d\n", ans);
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 2052
final_rank: 15