Problem:
B: VOCAL ANDROID
ボーカルアンドロイド (VOCAL ANDROID) とは，メロディと歌詞を入力して音声を合成する作曲ツールである． 2013年3月9日に和歌山県で開催された，ボーカルアンドロイドのライブに行ったあなたは，音楽の素晴らしさに感動した． そこで，あなたは新作ボーカルアンドロイド「立音リプロ (CV.立命太郎)」を購入し，作曲を始めることにした． ところが，メロディは完成したものの作詞で詰まってしまった．
使いたいフレーズはいくつか断片的に用意しており，それらを組み合わせて各メロディにあてはめる． 各メロディは，長さが決まっており，必ずこの長さぴったりになるようにフレーズをあてはめる必要がある． また，各フレーズには，そのフレーズを使うと得られるノリの良さを表す得点と，下限の長さと上限の長さが決まっている． フレーズは，長さが整数であれば，下限から上限まで自由に調整できる． ただし，長さを変更しても，得られる点数は変わらない． 図1は，サンプルの1番目の例である．
図1:
メロディに対するフレーズの割り当て
あなたの仕事はメロディの長さぴったりにフレーズを組み合わせて，できるだけ得点の総和が高い曲を作ることである． 同じフレーズは何度も使用してよいものとする．
Input
入力は以下の形式で与えられる．
n
s
1
l
1
p
1
…
s
n
l
n
p
n
m
w
1
…
w
m
1行目には
n
( 1 ≦
n
≦ 393 ) が与えられ， その後
n
行には歌詞に使うフレーズの情報が与えられる．
s
i
,
l
i
( 1 ≦
s
i
<
l
i
≦ 393 ) は， そのフレーズの長さの下限と上限であり，
p
i
( 1 ≦
p
i
≦ 393 ) は，そのフレーズの得点を表す．
m
( 1 ≦
m
≦ 393 ) は，メロディの数を表し， その後
m
行にメロディの長さ
w
i
( 1 ≦
w
i
≦ 393 ) が与えられる．
Output
以下のような形式で各メロディの得点を出力せよ．
1
行目 メロディ 1 の最大得点
…
m
行目 メロディ
m
の最大得点
ただし，1つでもフレーズをぴったり当てはめられないメロディが存在する場合には，-1のみを出力せよ．
Sample Input 1
3
4 5 15
2 3 4
7 8 39
2
6
8
Sample Output 1
19
39
Sample Input 2
1
2 3 10
2
3
1
Sample Output 2
-1

Submitted solutions:

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include<sstream>
#include<list>
#include<iomanip>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <bitset>
#include <cassert>
#include <stdlib.h>
#include <stdio.h>
#include <random>
#include <chrono>
using namespace std;
const long long LINF = 3e18 + 7;
const int MAX_N = 5000010;
const int MAX_W = 10002;
const int MAX_ARRAYK = 100000;
double PI = 3.14159265358979323846;
const int INF = 2147483647;
//using ll = long long;

int N, M;
int s[MAX_N], l[MAX_N], p[MAX_N];
int w[MAX_N];
int dp[410][410];

int main() {

	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> s[i] >> l[i] >> p[i];
	}

	cin >> M;
	for (int i = 0; i < M; i++) {
		cin >> w[i];
	}
	
	for (int i = 0; i <= N; i++) {
		for (int j = 0; j <= 400; j++) {
			dp[i][j] = -1;
		}
	}
	dp[0][0] = 0;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j <= 400; j++) {
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
			for (int k = s[i]; k <= l[i]; k++) {
				if (j - k >= 0 && dp[i][j - k] != -1) {
					dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - k] + p[i]);
				}
				if (j - k >= 0 && dp[i + 1][j - k] != -1) {
					dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - k] + p[i]);
				}
			}
		}
	}

	for (int i = 0; i < M; i++) {
		if (dp[N][w[i]] == -1) {
			cout << -1 << endl;
			return 0;
		}
	}
	
	for (int i = 0; i < M; i++) {
		cout << dp[N][w[i]] << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 10084
final_rank: 16

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;

using ll=long long;
using ld=long double;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vl=vector<ll>;
using vvl=vector<vl>;

#define rep(i,n) for(long long i=0LL;i<n;i++)
#define rrep(i,a,b) for(long long i=a;i>=b;i--)
#define FOR(i,k,n) for(long long i=k;i<n;i++)
#define ALL(v) v.begin(),v.end()
template<typename T>ostream&operator<<(ostream& os,vector<T>&v){for(int i = 0;i < int(size(v));i++)os << v[i] << (i+1==int(size(v))?"":" ");return os;}
template<typename T>istream&operator>>(istream& is,vector<T>&v){for(auto &in : v)is >> in;return is;}
ll INF = (1LL << 60) - 6846976;
/* INF = (1LL << 60) - 6846976*/


// #include<atcoder/all> 
// using namespace atcoder;
// using mint = modint998244353;ll mod = 998244353;
// // using mint = modint1000000007;ll mod = 1000000007;
// //using mint = modint;
// using vm = vector<mint>;
// using vvm = vector<vm>;
// ostream&operator<<(ostream& os,vector<mint>&v){for(int i = 0;i < int(size(v));i++)os << v[i].val() << (i+1==int(size(v))?"":" ");return os;}

#define LB(A,x) int(lower_bound(ALL(A),x)-A.begin())



// vl dx = {1,0,-1,0}, dy = {0,1,0,-1};
// vl dx = {1,1,1,0,-1,-1,-1,0},dy={-1,0,1,1,-1,0,1,-1};
//----------------------------------------------

int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);cout<<fixed<<setprecision(15);
//==============================================

  ll N;
  cin>>N;
  vl S(N),L(N),P(N);
  rep(i,N) cin>>S[i]>>L[i]>>P[i];

  vl dp(409);
  rep(i,N){
    rep(j,409){
      FOR(k,S[i],L[i]+1)if(j-k>=0){
        dp[j] = max(dp[j],dp[j-k]+P[i]);
      }
    }
  }




  ll M;
  cin>>M;
  vl ans(M);
  rep(i,M){
    ll w;cin>>w;
    if(!dp[w]){cout<<-1<<endl;return 0;}
    ans[i]=dp[w];
  }
  rep(i,M) cout<<ans[i]<<endl;




}
accuracy: 1
time_ms: 50
memory_kb: 3456
final_rank: 7

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

int main() {
    ll n;
    cin >> n;
    vector<ll> s(n + 1), l(n + 1), p(n + 1);
    for (ll i = 1; i <= n; i++) {
        cin >> s[i] >> l[i] >> p[i];
    }
    
    vector<vector<ll>> dp(n + 1, vector<ll> (394, -1));

    dp[0][0] = 0;
    for (ll i = 1; i <= n; i++) {
        for (ll j = 0; j <= 393; j++) {
            dp[i][j] = dp[i - 1][j];
            for (ll k = s[i]; k <= l[i]; k++) {
                if (j - k >= 0 && dp[i][j - k] >= 0) {
                    dp[i][j] = max(dp[i][j], dp[i][j - k] + p[i]);
                }
            }
        }
    }

    ll m;
    cin >> m;
    vector<ll> ans;
    while (m--) {
        ll w;
        cin >> w;
        if (dp[n][w] == -1) {
            cout << "-1" << '\n';
            return 0;
        }
        ans.push_back(dp[n][w]);
    }
    for (ll i = 0; i < ans.size(); i++) {
        cout << ans[i] << '\n';
    }
}
accuracy: 1
time_ms: 50
memory_kb: 4692
final_rank: 12

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include<sstream>
#include<list>
#include<iomanip>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <bitset>
#include <cassert>
#include <stdlib.h>
#include <stdio.h>
#include <random>
#include <chrono>
using namespace std;
const long long LINF = 3e18 + 7;
const int MAX_N = 5000010;
const int MAX_W = 10002;
const int MAX_ARRAYK = 100000;
double PI = 3.14159265358979323846;
const int INF = 2147483647;
//using ll = long long;

int N;
int s[MAX_N], l[MAX_N], p[MAX_N];
int M;
int w[MAX_N];
int dp[400][400];

int main() {
	
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> s[i] >> l[i] >> p[i];
	}

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

	for (int i = 0; i <= N; i++) {
		for (int j = 0; j <= 393; j++) {
			dp[i][j] = -1;
		}
	}

	dp[0][0] = 0;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j <= 393; j++) {
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
			for (int k = s[i]; k <= l[i]; k++) {
				if (j - k >= 0 && dp[i][j - k] != -1) {
					dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - k] + p[i]);
				}
				if (j - k >= 0 && dp[i + 1][j - k] != -1) {
					dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - k] + p[i]);
				}
			}
		}
	}

	vector<int> ans;
	for (int i = 0; i < M; i++) {
		if (dp[N][w[i]] != -1) {
			ans.push_back(dp[N][w[i]]);
		}
		else {
			cout << -1 << endl;
			return 0;
		}
	}

	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 10148
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define loop(n) for (int _i = 0; _i < (n); _i++)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n); i >= 0; --i)
#define rep2(i, r, n) for (int i = (r); i < (n); ++i)
#define all(obj) begin(obj), end(obj)
#define yes cout << "Yes"
#define no cout << "No"
using namespace std;
using ll = long long;
#define INF 1<<30
template<typename T> bool chmin(T &a,const T &b){return a>b? (a=b,true) : false;}
template<typename T> bool chmax(T &a,const T &b){return a<b? (a=b,true) : false;}

int main(){
    int n,m;
    cin >> n;
    vector<int> L(n),R(n),W(n);
    rep(i,n) cin >> L[i] >> R[i] >> W[i];
    vector<vector<int>> dp(n,vector<int>(394,0));
    rep(i,n) rep(j,394){
        if(i) dp[i][j] = dp[i-1][j];
        rep2(k,L[i],R[i]+1) if(j-k>=0) chmax(dp[i][j],dp[i][j-k]+W[i]);
    }
    cin >> m;
    vector<int> S(m);
    bool ok = true;
    rep(i,m) cin >> S[i];
    rep(i,m) {
        if(!dp[n-1][S[i]]) ok = false;
        S[i] = dp[n-1][S[i]];
    }
    if(ok) rep(i,m) cout << S[i] << endl;
    else cout << -1 << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3828
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define loop(n) for (int _i = 0; _i < (n); _i++)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n); i >= 0; --i)
#define rep2(i, r, n) for (int i = (r); i < (n); ++i)
#define all(obj) begin(obj), end(obj)
#define yes cout << "Yes"
#define no cout << "No"
using namespace std;
using ll = long long;
#define INF 1<<30
template<typename T>
bool chmin(T &a,const T &b){
    return a>b? (a=b,true) : false;
}
template<typename T>
bool chmax(T &a,const T &b){
    return a<b? (a=b,true) : false;
}

int main(){
    int n,m;
    cin >> n;
    vector<int> L(n),R(n),W(n);
    rep(i,n) cin >> L[i] >> R[i] >> W[i];
    vector<vector<int>> dp(n,vector<int>(394,0));
    rep(i,n) rep(j,394){
        if(i) dp[i][j] = dp[i-1][j];
        rep2(k,L[i],R[i]+1) if(j-k>=0) chmax(dp[i][j],dp[i][j-k]+W[i]);
    }
    cin >> m;
    vector<int> S(m);
    bool ok = true;
    rep(i,m) cin >> S[i];
    rep(i,m) {
        if(!dp[n-1][S[i]]) ok = false;
        S[i] = dp[n-1][S[i]];
    }
    if(ok) rep(i,m) cout << S[i] << endl;
    else cout << -1 << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3964
final_rank: 10

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

int main(){
    int N;
    cin >> N;

    vector<int> s(N), l(N), p(N);
    for(int i = 0; i < N; ++i){
        cin >> s[i] >> l[i] >> p[i];
    }

    vector<int> dp(400, -1);
    dp[0] = 0;
    for(int i = 0; i < 400; ++i){
        for(int j = 0; j < N; ++j){
            for(int k = s[j]; k <= l[j]; ++k){
                if(i - k >= 0){
                    dp[i] = max(dp[i], p[j] + dp[i - k]);
                }
            }
        }
    }

    int M;
    cin >> M;
    
    vector<int> w(M);
    vector<int> res(M, 0);
    bool flag = false;
    for(int i = 0; i < M; ++i){
        int w;
        cin >> w;
        res[i] = dp[w];
        if(res[i] == -1) flag = true;
    }

    if(flag){
        cout << -1 << endl;
        return 0;
    }
    
    for(int i = 0; i < M; ++i){
        cout << res[i] << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3456
final_rank: 7

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

int main(){
    int N;
    cin >> N;

    vector<int> s(N), l(N), p(N);
    for(int i = 0; i < N; ++i){
        cin >> s[i] >> l[i] >> p[i];
    }

    vector<int> dp(400, -1);
    dp[0] = 0;
    for(int i = 0; i < 400; ++i){
        for(int j = 0; j < N; ++j){
            for(int k = s[j]; k <= l[i]; ++k){
                if(i - k >= 0){
                    dp[i] = max(dp[i], p[j] + dp[i - k]);
                }
            }
        }
    }

    int M;
    cin >> M;
    
    vector<int> w(M);
    vector<int> res(M, 0);
    bool flag = false;
    for(int i = 0; i < M; ++i){
        int w;
        cin >> w;
        res[i] = dp[w];
        if(res[i] == -1) flag = true;
    }

    if(flag){
        cout << -1 << endl;
        return 0;
    }
    for(int i = 0; i < M; ++i){
        cout << res[i] << endl;
    }
    return 0;
}
accuracy: 0.17391304347826086
time_ms: 40
memory_kb: 3448
final_rank: 18

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

int main(){
    int N;
    cin >> N;

    vector<int> s(N), l(N), p(N);
    for(int i = 0; i < N; ++i){
        cin >> s[i] >> l[i] >> p[i];
    }

    vector<int> dp(400, -1);
    dp[0] = 0;
    for(int i = 0; i < 400; ++i){
        for(int j = 0; j < N; ++j){
            for(int k = s[j]; k <= l[i]; ++k){
                if(i - k >= 0){
                    dp[i] = max(dp[i], p[j] + dp[i - k]);
                }
            }
        }
    }

    int M;
    cin >> M;
    
    vector<int> w(M);
    for(int i = 0; i < M; ++i){
        cin >> w[i];
    }

    for(int i = 0; i < M; ++i){
        cout << dp[w[i]] << endl;
        if(dp[w[i]] == -1) break;
    }
    return 0;
}
accuracy: 0.08695652173913043
time_ms: 60
memory_kb: 3444
final_rank: 20

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <set>
#include <climits>
using namespace std;

int main() {
	int n, dp[394] {}, m;
	cin >> n;
	for (int i = 1; i <= 393; ++i) dp[i] = -1;
	for (int i = 0; i < n; ++i) {
		int s, l, p;
		cin >> s >> l >> p;
		for (int j = s; j <= l; ++j) {
			for (int k = j; k <= 393; ++k) {
				if (dp[k-j] == -1) continue;
				dp[k] = max(dp[k], dp[k-j] + p);
			}
		}
	}
	cin >> m;
	vector<int> v;
	for (int i = 0; i < m; ++i) {
		int a;
		cin >> a;
		if (dp[a] == -1) {
			cout << "-1\n";
			return 0;
		}
		v.push_back(dp[a]);
	}
	for (int i: v) cout << i << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 1

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

using namespace std;

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> s(n + 1), l(n + 1), p(n + 1);
  // 1-index でデータを保持
  for (int i = 1; i <= n; i++) {
    cin >> s[i] >> l[i] >> p[i];
  }

  int m;
  cin >> m;

  vector<int> w(m);
  for (int i = 0; i < m; i++) {
    cin >> w[i];
  }

  int W = *max_element(w.begin(), w.end());

  // dp[i][j] := i番目までのフレーズを使って長さがちょうどjとなるときの得点のMAX
  vector<vector<int>> dp(n + 1, vector<int>(W + 1, -1));
  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);

      for (int k = s[i]; k <= l[i]; k++) {
        if (j - k >= 0) {
          dp[i][j] = max(dp[i][j], dp[i][j - k] + p[i]);
        }
      }
    }
  }

  vector<int> ans;

  for (int i = 0; i < m; i++) {
    if (dp[n][w[i]] == -1) {
      cout << -1 << "\n";
      return 0;
    }
    ans.push_back(dp[n][w[i]]);
  }

  for (auto a : ans) {
    cout << a << "\n";
  }

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4096
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
const int inf = INT_MAX;
using ll = long long;
const ll INF = 1e18;
using P = pair<int,int>;
struct Edge{
    int to;
    ll cost;
    Edge(int to,ll cost):to(to),cost(cost){}
};
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<Edge>>;


struct UnionFind{
   vector<int> par,rank,siz;
   UnionFind(int n) : par(n,-1),rank(n,0),siz(n,1){ }

   int root(int x){
       if(par[x]==-1)return x;
       else return par[x]=root(par[x]);
   }

   bool issame(int x,int y){
       return root(x)==root(y);
   }

   bool unite(int x,int y){
       int rx=root(x),ry=root(y);
       if(rx==ry)return false;
       if(rank[rx]<rank[ry])swap(rx,ry);
       par[ry]=rx;
       if(rank[rx]==rank[ry])rank[rx]++;
       siz[rx]+=siz[ry];
       return true;
   }

   int size(int x){
       return siz[root(x)];
   }
};

class SegmentTree{
   public:
   vector<int>dat;
   int siz=1;

   void init(int N){
       siz=1;
       while(siz<N)siz*=2;
       dat=vector<int>(2*siz,0);
   }

   void update(int pos,int x){
       pos=pos+siz-1;
       dat[pos]=x;
       while(pos>=2){
           pos/=2;
           dat[pos]=max(dat[pos*2],dat[pos*2+1]);
       }
   }

   int query(int l,int r,int a,int b,int u){
       if(r<=a || b<=l)return -100000000;
       if(l<=a && b<=r)return dat[u];
       int m=(a+b)/2;
       int AnsL=query(l,r,a,m,u*2);
       int AnsR=query(l,r,m,b,u*2+1);
       return max(AnsL,AnsR);
   }
};

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

vector<int>dxs={1,0,-1,0};
vector<int>dys={0,1,0,-1};

//fixed << setprecision(10)
//A[i].erase(unique(ALL(A[i])),A[i].end());

int main(){
    int n;
    cin >> n;
    vector<int>s(n),l(n),p(n);
    rep(i,n)cin >> s[i] >> l[i] >> p[i];

    vector<int>dp(394,-1);
    dp[0]=0;
    rep(i,n){
        rep(j,394){
            if(dp[j]==-1)continue;
            for(int k=s[i];k<=l[i];k++){
                int v=j+k;
                if(v<394)chmax(dp[v],dp[j]+p[i]);
            }
        }
    }

    int m;
    cin >> m;
    vector<int>w(m);
    rep(i,m)cin >> w[i];
    vector<int>ans(m);
    rep(i,m){
        if(dp[w[i]]==-1){
            cout << -1 << endl;
            return 0;
        }
        ans[i]=dp[w[i]];
    }
    rep(i,m)cout << ans[i] << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3356
final_rank: 2

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
#include <unordered_set>
#include <functional>
using namespace std;
//using namespace atcoder;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
//void chmin(ll & a, ll b) { a = min(a, b); }
//using mint = modint998244353;
//using mint = modint1000000007;

int main() {
	int n;
	cin >> n;
	vector<int> s(n), l(n), p(n);
	rep(i, n) cin >> s[i] >> l[i] >> p[i];
	int m;
	cin >> m;
	vector<int> w(m);
	rep(i, m) cin >> w[i];
	vector<int> dp(400, -1);
	dp[0] = 0;
	rep(i, n) {
		rep(j, 400) {
			for (int k = s[i]; k <= l[i]; k++) {
				int pos = j + k;
				if (dp[j] != -1) {
					if (pos <= 393) {
						dp[pos] = max(dp[pos], dp[j] + p[i]);
					}
				}
			}
		}
	}

	rep(i, m) {
		if (dp[w[i]] == -1) {
			cout << dp[w[i]] << endl;
			return 0;
		}
	}

	rep(i, m) {
		cout << dp[w[i]] << endl;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 3492
final_rank: 4

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

using namespace std;
#define endl "\n"
typedef long long ll;
int n, m;
int f[394];
struct node {
  int l, r, k;
};
void solved() {
  memset(f, 0, sizeof f);
  cin >> n;
  node a[396];
  vector<int> b;
  bool ok = true;
  for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r >> a[i].k;
  for (int i = 1; i <= n; i++) {
    for (int j = a[i].l; j <= 395; j += a[i].l) {
      int cnt = a[i].r - a[i].l;
      for (int h = 0; h <= cnt; h++) {
        f[j + h] = max(f[j + h], f[j + h - a[i].l] + a[i].k);
      }
    }
  }
  cin >> m;
  while (m--) {
    int x;
    cin >> x;
    if (f[x] == 0) {
      ok = false;
      break;
    } else
      b.push_back(f[x]);
  }
  if (!ok)
    cout << -1 << endl;
  else {
    for (auto i : b) cout << i << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solved();

  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3468
final_rank: 14

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

using namespace std;
#define endl "\n"
typedef long long ll;
int n, m;
int f[394];
struct node {
  int l, r, k;
};
void solved() {
  memset(f, 0, sizeof f);
  cin >> n;
  node a[396];
  vector<int> b;
  bool ok = true;
  for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r >> a[i].k;
  for (int i = 1; i <= n; i++) {
    for (int j = a[i].l; j <= 395; j += a[i].l) {
      int cnt = a[i].r - a[i].l;
      for (int h = 0; h <= cnt; h++) {
        f[j + h] = max(f[j + h], f[j + h - a[i].l] + a[i].k);
      }
    }
  }
  cin >> m;
  while (m--) {
    int x;
    cin >> x;
    if (f[x] == 0) {
      ok = false;
      break;
    } else
      b.push_back(f[x]);
  }
  if (!ok)
    cout << -1 << endl;
  else {
    for (auto i : b) cout << i << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /*int t = 1;
  cin >> t;
  while (t--)*/
  solved();

  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3396
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;

int main(){
    int n;
    cin >> n;
    vector<int> s(n), l(n), p(n);
    for (int i = 0; i < n;i++){
        cin >> s.at(i) >> l.at(i) >> p.at(i);
    }
    // i番目のフレーズを考慮、長さjのメロディに対する最大価値
    vector<vector<int>> dp(n+1, vector<int>(400, -1));
    dp.at(0).at(0) = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 394;j++){
            for (int len = s.at(i); len <= l.at(i);len++){
                if ( j-len>=0){
                    dp.at(i + 1).at(j) = max(dp.at(i + 1).at(j), max(dp.at(i).at(j), dp.at(i + 1).at(j - len) + p.at(i)));
                }
                else{
                    dp.at(i + 1).at(j) = max(dp.at(i + 1).at(j), dp.at(i).at(j));
                }
            }
        }
    }

    int m;
    cin >> m;
    vector<int> w(m);
    for(int &p:w){
        cin >> p;
    }
    bool can = true;
    for (int i = 0; i < m; i++)
    {
        if(dp.at(n).at(w.at(i)) == -1){
            can = false;
        }
    }
    if(can){
        for (int i = 0; i < m;i++){
            cout << dp.at(n).at(w.at(i)) << endl;
        }
    }
    else{
        cout << -1 << endl;
    }
}
accuracy: 1
time_ms: 150
memory_kb: 3968
final_rank: 15

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

#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define Rrep(i,n) for(int i=n-1;i>=0;i--)
#define Rrep1(i,n) for(int i=n;i>0;i--)
#define all(a) a.begin(),a.end()
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vvvl=vector<vvl>;
#define INF ((ll)2e15)
#define IINF ((int)1e9)
const double PI = 3.1415926535897932384626433832795028841971;
template<typename T>inline bool chmax(T& a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T>inline bool chmin(T& a, T b) { return ((a > b) ? (a = b, true) : (false)); }
#define yesno(ans) cout<<((ans)?"Yes\n":"No\n")
bool eq(double a,double b){return abs(a-b)<0.0000001;}
const int di[4]={1,0,-1,0};
const int dj[4]={0,-1,0,1};

/*ACL
#include <atcoder/all>
using namespace atcoder;
//using mint = modint998244353;
using mint=modint1000000007;
using vm=vector<mint>;
using vvm=vector<vm>;
using vvvm=vector<vvm>;
//*/

int solve(){
    int N;cin>>N;
    vi S(N),L(N),P(N);
    rep(i,N)cin>>S[i]>>L[i]>>P[i];
    vi mxp(394,-1);
    rep(i,N){
        for(int j=S[i];j<=L[i];j++)chmax(mxp[j],P[i]);
    }
    int M;cin>>M;
    vi ans;
    rep(mi,M){
        int W;cin>>W;
        vi dp(W+1,-IINF);
        dp[0]=0;
        rep(i,394){
            if(mxp[i]==-1)continue;
            rep(j,W+1){
                int nj=j+i;
                if(nj<=W)chmax(dp[nj],dp[j]+mxp[i]);
            }
        }
        if(dp[W]<0){
            cout<<-1<<endl;
            return 0;
        }
        ans.push_back(dp[W]);
    }
    for(auto x:ans)cout<<x<<"\n";

    return 0;
}

int main()
{
    std::cin.tie(0)->sync_with_stdio(0);
    int T=1;
    //cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3504
final_rank: 5

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

void chmax(int &a, int b){a = max(a,b);}

int main(){
    int n;
    cin >> n;
    const int INF = 1e9;
    const int L = 2<<17;
    vector<int> dp(L+400,-INF);
    dp[0] = 0;
    rep(i,n){
        int l,r,p;
        cin >> l >> r >> p;
        vector<int> ndp = dp;
        rep(j,L+1){
            int njl = j + l; 
            int njr = j + r;
            chmax(ndp[njl],dp[njl]);
            chmax(ndp[njl],ndp[j]+p);
            chmax(ndp[njr],dp[njr]);
            chmax(ndp[njr],ndp[j]+p);
        }
        swap(dp,ndp);
    }
    int m;
    cin >> m;
    vector<int> ans;
    rep(mi,m){
        int w;
        cin >> w;
        if(dp[w] == -INF){
            cout << -1 << endl;
            return 0;
        } 
        else ans.push_back(dp[w]);
    }
    for(int x : ans) cout << x << '\n';
    return 0;
}
accuracy: 0.17391304347826086
time_ms: 50
memory_kb: 5156
final_rank: 19

code_snippet: #include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <bitset>
#include <algorithm>
#include <limits>
#include <cmath>
#include <queue>
#include <numeric>
#include <tuple>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <optional>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pint = pair<int, int>;
constexpr double PI = 3.141592653589793;
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr ll INF = 1LL << 60;
const vector<int> dy = { 0, 1, 0, -1 };
const vector<int> dx = { 1, 0, -1, 0 };
const vector<int> DY = { 0, 1, 1, 1, 0, -1, -1, -1 };
const vector<int> DX = { 1, 1, 0, -1, -1, -1, 0, 1 };
#define vec2d(T, name, n, m, ini) vector<vector<T>> name(n, vector<T>(m, ini))
#define vec3d(T, name, n, m, l, ini) vector<vector<vector<T>>> name(n, vector<vector<T>>(m, vector<T>(l, ini)))
#define REP(i, e) rep(i, 0, e)
#define rep(i, s, e) for (ll i = s; i < (e); ++i)
#define rrep(i, s, e) for (ll i = s; i >= (e); --i)
#define all(x) (x).begin(), (x).end()

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

	int n, m;
	cin >> n;
	vector<int> s(n + 1), l(n + 1), p(n + 1);
	rep(i, 1, n + 1) cin >> s[i] >> l[i] >> p[i];

	vec2d(int, dp, n + 1, 394, -1);
	dp[0][0] = 0;
	rep(i, 1, n + 1) rep(j, 0, 394) {
		dp[i][j] = max(dp[i][j], dp[i - 1][j]);
		if (dp[i][j] != -1 && j + s[i] <= 393) {
			rep(k, j + s[i], min(int(j) + l[i] + 1, 394))
				dp[i][k] = max(dp[i][k], dp[i][j] + p[i]);
		}
	}
	
	cin >> m;
	vector<int> ans;
	REP(i, m) {
		int w;
		cin >> w;
		if (dp[n][w] == -1) {
			cout << dp[n][w] << "\n";
			return 0;
		}
		else {
			ans.push_back(dp[n][w]);
		}
	}
	for (auto v : ans) {
		cout << v << "\n";
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3860
final_rank: 3

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

class RangeMax {
public:
    int size_ = 1;
    vector<long long> dat;

    void init(int sz) {
        while (size_ <= sz) size_ *= 2;
        dat.resize(size_ * 2, -(1LL << 60));
    }
    void update(int pos, long long x) {
        pos += size_;
        dat[pos] = x;
        while (pos >= 2) {
            pos >>= 1;
            dat[pos] = max(dat[pos * 2], dat[pos * 2 + 1]);
        }
    }
    long long query_(int l, int r, int a, int b, int u) {
        if (l <= a && b <= r) return dat[u];
        if (r <= a || b <= l) return -(1LL << 60);
        long long v1 = query_(l, r, a, (a + b) >> 1, u * 2);
        long long v2 = query_(l, r, (a + b) >> 1, b, u * 2 + 1);
        return max(v1, v2);
    }
    long long query(int l, int r) {
        return query_(l, r, 0, size_, 1);
    }
};

int main() {
    int n, m;
    cin >> n;
    vector<int> s(n), l(n), p(n);
    for (int i = 0; i < n; ++i) {
        cin >> s[i] >> l[i] >> p[i];
    }

    cin >> m;
    vector<int> w(m);
    for (int i = 0; i < m; ++i) {
        cin >> w[i];
    }

    vector<long long> dp(400, -1);
    dp[0] = 0;

    for (int i = 1; i < 400; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int k = s[j]; k <= l[j]; ++k) {
                if (i - k >= 0) {
                    dp[i] = max(dp[i], dp[i - k] + p[j]);
                }
            }
        }
    }

    for (int i = 0; i < m; ++i) {
        if (dp[w[i]] < 0) {
            cout << -1 << endl;
            return 0;
        }
    }

    for (int i = 0; i < m; ++i) {
        cout << dp[w[i]] << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3456
final_rank: 7