Problem:
Make KND So Fat
Problem
KND君は会津大学に在籍する学生プログラマである。彼の隣人はとても迷惑なことで知られている。隣人は彼が甘党であることを知り、必要以上に甘味を差し入れて彼を太らせようと画策している。そのために、当の隣人であるあなたは友人からとある甘味処を紹介してもらった。
しかし、その甘味処での商品の販売の仕方は少し変わっている。その販売方法とは、甘味セットと呼ばれる甘味の組を日替わりで販売するというものだ。予算内であればその日に販売されている甘味セットの中から自由に甘味を買うことができる。しかし、甘味セットに含まれる甘味は一種類につき１個しか販売されていない。
また、予算は期間の始めに用意した額から支払い、期間途中での追加予算はないものとする。あなたは友人から教えられた甘味の体重に与える影響の度合いと値段をもとに、ある期間中に一定の予算内で彼の体重を最適に増やす甘味の買い方を計算するプログラムを作成することにした。
Input
入力は複数のテストケースからなる。空行区切りで与えられる。ひとつのテストケースは以下の形式で与えられる。入力の終了をEOFで示す。
s
d
m
k
1
w
1,1
p
1,1
w
1,2
p
1,2
...
w
1,k
p
1,k
k
2
w
2,1
p
2,1
w
2,2
p
2,2
...
w
2,k
p
2,k
...
k
s
w
s,1
p
s,1
w
s,2
p
s,2
...
w
s,k
p
s,k
f
1
f
2
...
f
d
ここで、
s
:甘味セットの種類
d
:対象となる期間
m
:予算
k
i
:i番目の甘味セットに含まれる甘味の種類数
w
i,j
:i番目の甘味セットに含まれるj番目の甘味の体重に与える影響
p
i,j
:i番目の甘味セットに含まれるj番目の甘味の値段
f
i
:i日目に販売されている甘味セットの番号(0から始まる)
である。
Constraints
入力は以下の条件を満たす。
入力はすべて整数。
1 ≤
s
≤
d
≤ 100
1 ≤
m
≤ 300
1 ≤
k
i
≤ 50
0 ≤
w
i,j
≤ 50
0 ≤
p
i,j
≤ 300
0 ≤
f
i
<
s
Output
各テストケースにつき予算内で体重に与えられる最大の影響とその影響を与えるために必要な最小の金額を空白区切りで1行に出力せよ。
Sample Input
3 3 100
2
5 20 3 10
2
10 50 12 60
2
8 30 22 100
0 1 2

1 1 30
1
13 8
0
Sample Output
23 100
13 8

Submitted solutions:

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int s,d,m;
    while (cin >> s >> d >> m) {
        vector<vector<pair<int,int>>> v(s);
        for (int i = 0; i < s; ++i) {
            int z; cin >> z; v[i].resize(z);
            for (int j = 0; j < z; ++j) {
                cin >> v[i][j].first >> v[i][j].second;
            }
        }
        vector<pair<int,int>> x;
        for (int i = 0; i < d; ++i) {
            int f; cin >> f;
            for (auto j : v[f]) {
                x.push_back(j);
            }
        }
        int n = x.size();
        vector<int> dp(m+1, -1e9);
        dp[0] = 0;
        for (int i = 0; i < n; ++i) {
            vector<int> ndp = dp;
            for (int j = x[i].second; j <= m; ++j) {
                ndp[j] = max(ndp[j], dp[j-x[i].second] + x[i].first);
            }
            swap(dp, ndp);
        }
        int mx = -1, res = 0;
        for (int i = 0; i <= m; ++i) {
            if (mx < dp[i]) {
                mx = dp[i]; res = i;
            }
        }
        cout << mx << " " << res << endl;
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3536
final_rank: 2

code_snippet: #include<iostream>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<stack>
#include<functional>
#define INF ((1 << 31) - 1)
#define MOD 998244353
#define EPS 0.000001
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
using namespace std;
typedef long long ll;
int dp[105][305], k[105];
int w[105][105], p[105][105];
int main(void)
{
	int s, d, m;
	while (cin >> s >> d >> m)
	{
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= s; i++)
		{
			cin >> k[i];
			for (int j = 1; j <= k[i]; j++)
			{
				cin >> w[i][j] >> p[i][j];
			}
		}
		for (int i = 1; i <= d; i++)
		{
			int f;
			cin >> f;
			f++;
			for (int j = 1; j <= k[f]; j++)
			{
				for (int q = m; q >= p[f][j]; q--)
				{
					dp[i][q] = max(max(dp[i - 1][q], dp[i - 1][q - p[f][j]] + w[f][j]), max(dp[i][q], dp[i][q - p[f][j]] + w[f][j]));
				}
				for (int q = 0; q < p[f][j]; q++)
				{
					dp[i][q] = max(dp[i][q], dp[i - 1][q]);
				}
			}
			/*for (int q = 0; q <= m; q++)
				cout << dp[i][q] << " ";
			cout << endl;*/
		}
		int ans = m;
		for (int i = m; i >= 0; i--)
			if (dp[d][i] == dp[d][m])
				ans = i;
		cout << dp[d][m] << " " << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 3216
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
int k[105];
int w[105][55];
int p[105][55];
int f[105];
int s,d,m;
int dp[305];
int main()
{

    while(cin>>s>>d>>m){
        for(int i=0;i<s;i++){
            cin>>k[i];
            for(int j=0;j<k[i];j++){
                cin>>w[i][j]>>p[i][j];
            }
        }
        for(int i=0;i<d;i++){
            cin>>f[i];
        }
        dp[0]=0;
        for(int i=1;i<305;i++){
            dp[i]=-1000000;
        }
        for(int i=0;i<d;i++){
            int n=f[i];
            for(int j=0;j<k[n];j++){
                for(int l=m;l>=p[n][j];l--){
                    dp[l]=max(dp[l],dp[l-p[n][j]]+w[n][j]);
                }
            }
        }
        int mx=0,ind=0;
        for(int i=1;i<=m;i++){
            if(dp[i]>mx){
                mx=dp[i];
                ind=i;
            }
        }
        cout<<dp[ind]<<" "<<ind<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3144
final_rank: 7

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-11, PI = acos(-1);
//ここから編集

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int s, d, m;
  while(cin >> s >> d >> m){
    
    vector<int> k(s);
    vector<vector<pair<int, int>>> w(s);
    REP(i,s){
      cin >> k[i];
      for(int j=0; j<k[i]; j++){
        int ww, p; cin >> ww >> p;
        w[i].push_back({ww, p});
      }
    }

    vector<int> f(d);
    REP(i,d) cin >> f[i];

    int dp[301] = {0};
    for(int i=0; i<d; i++){

      for(int j=0; j<w[f[i]].size(); j++){

        for(int x=m; x>=0; x--){
          if(x+w[f[i]][j].second <= m){
            dp[x+w[f[i]][j].second] = max(dp[x+w[f[i]][j].second], dp[x] + w[f[i]][j].first);
          }
        }
      }
    }
    int ans = 0;
    int kakaku = 0;
    for(int i=0; i<=m; i++) {
      if(ans < dp[i]){
        ans = dp[i];
        kakaku = i;
      }
    }
    cout << ans << " " << kakaku << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3196
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
struct Sweet {
	int weight, price;
};
int main() {
	int s, d, m;
	while (std::cin >> s >> d >> m) {
		std::vector<int> max_weight(m + 1, 0);
		std::vector<std::vector<Sweet>> sweets(s);
		for (auto &sweet : sweets) {
			int k; std::cin >> k;
			for (auto i = 0; i < k; ++i) {
				Sweet sw; std::cin >> sw.weight >> sw.price;
				sweet.push_back(sw);
			}
		}
		for (auto _i = 0; _i < d; ++_i) {
			int f; std::cin >> f;
			for (auto sweet : sweets[f]) {
				for (int i = m - sweet.price; i >= 0; --i) {
					max_weight[i + sweet.price] = std::max(max_weight[i + sweet.price], max_weight[i] + sweet.weight);
				}
			}
		}
		std::cout << max_weight.back() << ' ' << std::distance(max_weight.begin(), std::lower_bound(max_weight.begin(), max_weight.end(), max_weight.back())) << '\n';
	}
}
accuracy: 1
time_ms: 130
memory_kb: 3132
final_rank: 5

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...)); }
    // clang-format on

#include <utility>
using K = pair<int, int>;

template<typename T>
void chmax(T &a, T b) {
    a = max(a, b);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int s, d, m;
    while (cin >> s >> d >> m) {
        vector<vector<K>> Kanmi(s);
        REP(i, 0, s) {
            int k;
            cin >> k;
            REP(j, 0, k) {
                int a, b;
                cin >> a >> b;
                Kanmi[i].emplace_back(a, b);
            }
        }
        vector<K> Lst;
        {
            REP(i, 0, d) {
                int f;
                cin >> f;
                for (auto L : Kanmi[f]) {
                    Lst.push_back(L);
                }
            }
        }
        auto dp = make_v(Lst.size() + 1, m + 1, 0LL);
        REP(i, 0, Lst.size()) {
            REP(j, 0, m + 1) {
                chmax(dp[i + 1][j], dp[i][j]);
                if (j + Lst[i].second <= m) {
                    chmax(dp[i + 1][j + Lst[i].second], dp[i][j] + Lst[i].first);
                }
            }
        }
        ll ans = -1;
        int prm = -1;
        REP(i, 0, m + 1) {
            if (ans < dp[Lst.size()][i]) {
                ans = dp[Lst.size()][i];
                prm = i;
            }
        }
        cout << ans << " " << prm << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 8704
final_rank: 16

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

int main(){
    int s, d, m, k, w, p, f;

    while(cin >> s >> d >> m){
        int dp[301] = {};

        vector<pair<int,int>> v[s];
        for(int i = 0; i < s; i++){
            cin >> k;
            while(k-- > 0){
                cin >> w >> p;
                v[i].push_back({w,p});
            }
        }

        map<pair<int,int>,int> ma;

        for(int i = 0; i < d; i++){
            cin >> f;
            for(pair<int,int> price : v[f]) ma[price]++;
        }

        for(auto x : ma){
            pair<int,int> price = x.first;
            for(int i = 0; i < ma[price]; i++){
                // 2進を使って効率よくやった方が良いかもしれないが、面倒なのでさぼる
                for(int j = m; j >= price.second; j--){
                    dp[j] = max(dp[j], dp[j-price.second]+price.first);
                }
            }
        }

        int a = 0, b = 0;
        for(int i = 0; i <= m; i++){
            if(dp[i] > a){
                a = dp[i], b = i;
            }
        }
        cout << a << " " << b << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3184
final_rank: 10

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

typedef struct{
  int k;
  int w[51];
  int p[51];
} Data;

int s,b,m;

typedef struct{
  int kingaku;
  int max_num;
} Ans;

Ans memo[110][60][310];

Ans dfs(Data d[], int f[], int i, int j, int nokori){
  Ans ans;
  if(i == b){
    ans.kingaku = 0;
    ans.max_num = 0;
    return ans;
  }

  if(memo[i][j][nokori].max_num != -1)
    return memo[i][j][nokori];

  int nexti = i;
  int nextj = j+1;

  if(j == d[f[i]].k){
    nextj = 0; nexti++;
  }

  ans = dfs(d,f,nexti,nextj,nokori);
  Ans tmp;

  if(nokori >= d[f[i]].p[j]){
    tmp = dfs(d,f,nexti,nextj,nokori-d[f[i]].p[j]);
    tmp.max_num += d[f[i]].w[j];
    tmp.kingaku += d[f[i]].p[j];
    if(tmp.max_num > ans.max_num){
      ans = tmp;
    }else if(tmp.max_num == ans.max_num && tmp.kingaku < ans.kingaku){
      ans = tmp;
    }
  }

  return memo[i][j][nokori] = ans;
}


int main(){

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  Ans a;

  while(cin >> s >> b >> m){
    Data d[110] = {};
    int f[110] = {};
    for(int i = 0;i < s;i++){
      cin >> d[i].k;
      for(int j = 0;j < d[i].k;j++){
        cin >> d[i].w[j] >> d[i].p[j];
      }
    }

    for(int i = 0;i < b;i++){
      cin >> f[i];
    }

    memset(memo,-1,sizeof(Ans)*110*60*310);

    a = dfs(d,f,0,0,m);
    cout << a.max_num << " " << a.kingaku << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 1140
memory_kb: 19472
final_rank: 20

code_snippet: #include <stdio.h>
#include <cmath>
#include <math.h>
#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;


struct Info{
	int value,cost;
};

Info info[100][50];
int item_num[100],sell_count[100];
int dp[301],next_dp[301];

int main(){

	int S,D,M;

	while(scanf("%d %d %d",&S,&D,&M) != EOF){

		for(int i = 0; i < S; i++)sell_count[i] = 0;

		for(int i = 0; i < S; i++){
			scanf("%d",&item_num[i]);

			for(int k = 0; k < item_num[i]; k++){
				scanf("%d %d",&info[i][k].value,&info[i][k].cost);
			}
		}

		int tmp;
		for(int i = 0; i < D; i++){
			scanf("%d",&tmp);
			sell_count[tmp]++;
		}

		dp[0] = 0;
		next_dp[0] = 0;
		for(int i = 1; i <= M; i++){
			dp[i] = -1;
			next_dp[i] = -1;
		}

		vector<int> Updated;

		for(int i = 0; i < S; i++){
			if(sell_count[i] == 0)continue;

			for(int k = 0; k < item_num[i]; k++){
				for(int num = 1; num <= sell_count[i]; num++){
					for(int budget = M; budget-num*info[i][k].cost >= 0; budget--){
						if(dp[budget-num*info[i][k].cost] >= 0){
							if(next_dp[budget] < dp[budget-num*info[i][k].cost]+num*info[i][k].value){
								next_dp[budget] = dp[budget-num*info[i][k].cost]+num*info[i][k].value;
								Updated.push_back(budget);
							}
						}
					}
				}
				for(int p = 0; p < Updated.size(); p++){
					dp[Updated[p]] = next_dp[Updated[p]];
				}
				Updated.clear();
			}
		}

		int max_effect = -1,min_budget;
		for(int budget = 0; budget <= M; budget++){
			if(max_effect < dp[budget]){
				max_effect = dp[budget];
				min_budget = budget;
			}
		}

		printf("%d %d\n",max_effect,min_budget);
	}
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3252
final_rank: 6

code_snippet: #include <stdio.h>
#include <cmath>
#include <math.h>
#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;


struct Info{
	int value,cost;
};

Info info[100][50];
int item_num[100],sell_count[100];
int dp[301],next_dp[301];

int main(){

	int S,D,M;

	while(scanf("%d %d %d",&S,&D,&M) != EOF){

		for(int i = 0; i < S; i++)sell_count[i] = 0;

		for(int i = 0; i < S; i++){
			scanf("%d",&item_num[i]);

			for(int k = 0; k < item_num[i]; k++){
				scanf("%d %d",&info[i][k].value,&info[i][k].cost);
			}
		}

		int tmp;
		for(int i = 0; i < D; i++){
			scanf("%d",&tmp);
			sell_count[tmp]++;
		}

		dp[0] = 0;
		next_dp[0] = 0;
		for(int i = 1; i <= M; i++){
			dp[i] = -1;
			next_dp[i] = -1;
		}

		for(int i = 0; i < S; i++){
			if(sell_count[i] == 0)continue;

			for(int k = 0; k < item_num[i]; k++){
				for(int num = 1; num <= sell_count[i]; num++){
					for(int budget = M; budget-num*info[i][k].cost >= 0; budget--){
						if(dp[budget-num*info[i][k].cost] >= 0){
							next_dp[budget] = max(next_dp[budget],dp[budget-num*info[i][k].cost]+num*info[i][k].value);
						}
					}
				}
				for(int budget = 0; budget <= M; budget++)dp[budget] = next_dp[budget];
			}
		}

		int max_effect = -1,min_budget;
		for(int budget = 0; budget <= M; budget++){
			if(max_effect < dp[budget]){
				max_effect = dp[budget];
				min_budget = budget;
			}
		}

		printf("%d %d\n",max_effect,min_budget);
	}
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3216
final_rank: 4

code_snippet: #include <stdio.h>
#include <cmath>
#include <math.h>
#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;


struct Info{
	int value,cost;
};

Info info[100][50];
int item_num[100];
int dp[301];

int main(){

	int S,D,M;

	while(scanf("%d %d %d",&S,&D,&M) != EOF){

		for(int i = 0; i < S; i++){
			scanf("%d",&item_num[i]);

			for(int k = 0; k < item_num[i]; k++){
				scanf("%d %d",&info[i][k].value,&info[i][k].cost);
			}
		}

		dp[0] = 0;
		for(int i = 1; i <= M; i++)dp[i] = -1;


		int tmp;
		for(int loop = 0; loop < D; loop++){
			scanf("%d",&tmp);

			for(int k = 0; k < item_num[tmp]; k++){
				for(int budget = M; budget-info[tmp][k].cost >= 0; budget--){
					if(dp[budget-info[tmp][k].cost] >= 0){
						dp[budget] = max(dp[budget],dp[budget-info[tmp][k].cost]+info[tmp][k].value);
					}
				}
			}
		}

		int max_effect = -1,min_budget;
		for(int budget = 0; budget <= M; budget++){
			if(max_effect < dp[budget]){
				max_effect = dp[budget];
				min_budget = budget;
			}
		}

		printf("%d %d\n",max_effect,min_budget);
	}
	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3156
final_rank: 1

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

main(){
  int s,d,m;
  while(cin>>s>>d>>m){

    int dp[5001][301]={},pp=0,ppp=100000000,k,w,p,cc;

    vector<pair<int,int> >a[101],c;

    for(int i=0;i<s;i++){
      cin>>k;

      for(int j=0;j<k;j++){
        cin>>w>>p;
        a[i].push_back(mk(p,w));

      }
    }
    for(int i=0;i<d;i++){
      cin>>cc;

      for(int j=0;j<a[cc].size();j++)
        c.push_back(a[cc][j]);
    }
    for(int i=1;i<=c.size();i++)
     for(int j=0;j<=m;j++){

      if(j<c[i-1].first)
        dp[i][j]=dp[i-1][j];
      else
         dp[i][j]=max(dp[i-1][j-c[i-1].first]+c[i-1].second,dp[i-1][j]);

      pp=max(pp,dp[i][j]);
     }

    for(int i=0;i<=c.size();i++)
      for(int j=0;j<=m;j++)
        if(pp==dp[i][j])ppp=min(ppp,j);

    cout<<pp<<' '<<ppp<<endl;
  }
}
accuracy: 1
time_ms: 240
memory_kb: 9044
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define mk make_pair
int main(){
  int s,d,m;
  while(cin>>s>>d>>m){
    int dp[5001][301]={},pp=0,ppp=100000000,k,w,p,cc;
    vector<pair<int,int> >a[101],c;
    for(int i=0;i<s;i++){
      cin>>k;
      for(int j=0;j<k;j++){
        cin>>w>>p;
        a[i].push_back(mk(p,w));
      }
    }
    for(int i=0;i<d;i++){
      cin>>cc;
      for(int j=0;j<a[cc].size();j++)
        c.push_back(a[cc][j]);
    }
    for(int i=1;i<=c.size();i++)
     for(int j=0;j<=m;j++){
      if(j<c[i-1].first)
        dp[i][j]=dp[i-1][j];
      else
         dp[i][j]=max(dp[i-1][j-c[i-1].first]+c[i-1].second,dp[i-1][j]);
      pp=max(pp,dp[i][j]);
     }
     /*for(int i=0;i<=c.size();i++){
      for(int j=0;j<=m;j++)cout<<dp[i][j]<<' ';
        cout<<endl;
     }*/
    for(int i=0;i<=c.size();i++)
      for(int j=0;j<=m;j++)
        if(pp==dp[i][j])ppp=min(ppp,j);
    cout<<pp<<' '<<ppp<<endl;
  }
}
accuracy: 1
time_ms: 240
memory_kb: 9068
final_rank: 18

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

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

int s, d, m;
int k[303];
int w[303][303], p[303][303];
int f[303];

int dp[303][303];
int dp2[303][303];

signed main(){
  while(cin >> s >> d >> m) {
    rep(i, s) {
      cin >> k[i];
      rep(j, k[i]) cin >> w[i][j] >> p[i][j];
      //rep(j, k[i]) cout<<w[i][j]<<"-"<<p[i][j]<<endl;
    }
    rep(i, d) cin >> f[i];
    memset(dp, -1, sizeof(dp));
    rep(i, s) {
      dp[i][0] = 0;
      rep(j, k[i]) {
	for(int l = m; l >= p[i][j]; l--) {
	  if(dp[i][l-p[i][j]] == -1) continue;
	  dp[i][l] = max(dp[i][l], dp[i][l-p[i][j]] + w[i][j]);
	}
      }
      //for(int j=0;j<=m;j++) cout<<j<<":"<<dp[i][j]<<endl;
    }
    memset(dp2, -1, sizeof(dp2));
    dp2[0][0] = 0;
    rep(i, d) {
      //cout<<i<<":"<<f[i]<<endl;
      //rep(j, m+1) {
      for(int j = m; j >= 0; j--) {
	for(int l = m; l >= j; l--) {
	  if(dp2[i][l-j] == -1 || dp[f[i]][j] == -1) continue;
	  dp2[i+1][l] = max(dp2[i+1][l], dp2[i][l-j] + dp[f[i]][j]);
	}
      }
    }
    int ansp, answ = -1;
    rep(i, m+1) {
      //cout << i << " "<<dp2[i]<<endl;
      if(answ < dp2[d][i]) {
	answ = dp2[d][i];
	ansp = i;
      }
    }
    cout << answ << " " << ansp << endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 460
memory_kb: 4960
final_rank: 15

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



int main(){
  
  int s,d,m;
  while(cin>>s>>d>>m){
    
    vector<P> v[N],cost;
    
    for(int i=0;i<s;i++){
    
      int k;
      cin>>k;

      for(int j=0;j<k;j++){
      
	int w,p;
	cin>>w>>p;
      
	v[i].push_back(P(w,p));
      }
    
    }

    for(int i=0;i<d;i++){
    
      int f;
      cin>>f;
    
      for(int j=0;j<v[f].size();j++)
	cost.push_back(v[f][j]);
    
    }

    int dp[5005][305];

    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
  
    for(int i=0;i<cost.size();i++){

      for(int j=0;j<=m;j++){

	if(dp[i][j]==-1)continue;
      
	dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
      
	if(j+cost[i].second<=m)
	  dp[i+1][j+cost[i].second]=max(dp[i+1][j+cost[i].second],dp[i][j]+cost[i].first);
      
      }
    
    }

    int ans1=0,ans2=0;

    for(int i=m;i>=0;i--){
      
      if(ans1<=dp[cost.size()][i]){
	ans1=dp[cost.size()][i];
	ans2=i;
      }
    }

    cout<<ans1<<' '<<ans2<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 9124
final_rank: 19

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int s, d, m;
int main() {
	while (~scanf("%d%d%d", &s, &d, &m)) {
		vector<vector<pair<int, int> > > v(s);
		for (int i = 0; i < s; i++) {
			int z; scanf("%d", &z); v[i].resize(z);
			for (int j = 0; j < z; j++) scanf("%d%d", &v[i][j].first, &v[i][j].second);
		}
		vector<pair<int, int> > x;
		for (int i = 0; i < d; i++) {
			int f; scanf("%d", &f);
			x.insert(x.end(), v[f].begin(), v[f].end());
		}
		int n = x.size();
		vector<vector<int> > dp(n + 1, vector<int>(m + 1, -99999999));
		dp[0][0] = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				dp[i][j] = dp[i - 1][j];
				if (j >= x[i - 1].second) dp[i][j] = max(dp[i][j], dp[i - 1][j - x[i - 1].second] + x[i - 1].first);
			}
		}
		int ret1 = 0, ret2 = 0;
		for (int i = 0; i <= m; i++) {
			if (dp[n][i] > ret1) ret1 = dp[n][i], ret2 = i;
		}
		printf("%d %d\n", ret1, ret2);
	}
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 6032
final_rank: 12

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int s, d, m;
int main() {
	while (cin >> s >> d >> m) {
		vector<vector<pair<int, int> > > v(s);
		for (int i = 0; i < s; i++) {
			int z; cin >> z; v[i].resize(z);
			for (int j = 0; j < z; j++) cin >> v[i][j].first >> v[i][j].second;
		}
		vector<pair<int, int> > x;
		for (int i = 0; i < d; i++) {
			int f; cin >> f;
			x.insert(x.end(), v[f].begin(), v[f].end());
		}
		int n = x.size();
		vector<vector<int> > dp(n + 1, vector<int>(m + 1, -99999999));
		dp[0][0] = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				dp[i][j] = dp[i - 1][j];
				if (j >= x[i - 1].second) dp[i][j] = max(dp[i][j], dp[i - 1][j - x[i - 1].second] + x[i - 1].first);
			}
		}
		int ret1 = 0, ret2 = 0;
		for (int i = 0; i <= m; i++) {
			if (dp[n][i] > ret1) ret1 = dp[n][i], ret2 = i;
		}
		cout << ret1 << ' ' << ret2 << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 5872
final_rank: 13

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class t>
using table = vector<vector<t>>;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"
  


int main() {
	int S, D, M;
	while (cin >> S >> D >> M) {
		map<pair<int, int>, int>nums;
		vector<vector<pair<int, int>>>sws(S);
		for (int i = 0; i < S; ++i) {
			int k; cin >> k;
			vector<pair<int, int>>sw(k);
			for (int j = 0; j < k; ++j) {
				int w, p; cin >> w >> p;
				sw[j] = make_pair(w, p);
			}
			sws[i] = sw;
		}
		map<int, int>mp;
		for (int i = 0; i < D; ++i) {
			int f; cin >> f;
			mp[f]++;
		}
		for (int i = 0; i < S; ++i) {
			for (int j = 0; j < sws[i].size(); ++j) {
				nums[sws[i][j]] += mp[i];
			}
		}

		vector<vector<int>>memo(nums.size() + 1, vector<int>(M + 1,-1e8));
		memo[0][0] = 0;
		int aa = 0;
		for (auto num:nums) {
			for (int nweight = 0; nweight<= M; ++nweight) {
				for (int i = 0; i <= num.second; ++i) {
					const int nextweight = nweight + num.first.second*i;
					const int nextvalue = memo[aa][nweight] + num.first.first*i;
					if (nextweight <= M) {
						memo[aa + 1][nextweight] = max(memo[aa + 1][nextweight], nextvalue);
					}
					else {
						break;
					}
				}
			}
			
			aa++;
		}

		int ans = 0;
		int ans2 = 0;
		for (int i = 0; i <= M; ++i) {
			if (ans < memo[nums.size()][i]) {
				ans = memo[nums.size()][i];
				ans2 = i;
			}
		}
		cout << ans<<" "<<ans2 << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 5380
final_rank: 14

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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int INF=12345678;

//weight,price
typedef pair<int,int> pi;

int main()
{
    int s,d,m;
    while(cin >>s >>d >>m)
    {
        vector<vector<pi>> kanmi(s);
        vector<int> f(d);

        rep(i,s)
        {
            int k;
            cin >>k;
            rep(j,k)
            {
                int w,p;
                cin >>w >>p;
                kanmi[i].pb(pi(w,p));
            }
        }
        rep(i,d) cin >>f[i];

        //i????????£??????????¢????????????????????????§???
        int dp[301]={0};

        rep(i,d)
        {
            //????????\???j??????????????????????????????
            rep(j,kanmi[f[i]].size())
            {
                for(int x=m; x>=0; --x)
                {
                    //?????????
                    if(x+kanmi[f[i]][j].se<=m)
                    {
                        dp[x+kanmi[f[i]][j].se] = max(dp[x+kanmi[f[i]][j].se], dp[x]+kanmi[f[i]][j].fi);
                    }
                }

            }
        }

        int ax=0,ay=0;
        rep(i,m+1)
        {
            //printf("dp[%d]=%d\n", i,dp[i]);
            if(dp[i]>ax)
            {
                ax=dp[i];
                ay=i;
            }
        }

        printf("%d %d\n",ax,ay);
    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3156
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define MAX 334
typedef pair<int, int> pii;

int main()
{
    int s, d, m;
    while (cin >> s >> d >> m) {
        vector<pii> v[s];
        for (int i = 0; i < s; i++) {
            int k, w, p;
            cin >> k;
            for (int j = 0; j < k; j++) {
                cin >> w >> p;
                v[i].push_back(pii(w, p));
            }
        }

        int dp[MAX], f;
        memset(dp, -1, sizeof(dp));
        dp[m] = 0;
        for (int i = 0; i < d; i++) {
            cin >> f;
            for (int j = 0; j < (int)v[f].size(); j++) {
                for (int k = 0; k <= m; k++) {
                    if (dp[k] == -1) continue;
                    int nm = k - v[f][j].second;
                    if (nm >= 0) {
                        dp[nm] = max(dp[nm], dp[k] + v[f][j].first);
                    }
                }
            }   
        }
        
        int idx = m;
        for (int i = m-1; i >= 0; i--) {
            if (dp[i] > dp[idx]) {
                idx = i;
            }
        }
        cout << dp[idx] << " " << m - idx << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 3120
final_rank: 9