Go: ///
// File:  2502.go
// Author: ymiyamoto
//
// Created on Sat Mar 24 00:44:45 2018
//
package main

import (
	"fmt"
)

var n, m int

type phrase struct {
	s, l, p int
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func main() {
	fmt.Scan(&n)

	phrases := make([]phrase, n)
	max_count := 0
	for i := 0; i < n; i++ {
		fmt.Scan(&phrases[i].s, &phrases[i].l, &phrases[i].p)
		max_count += phrases[i].l
	}

	dp := make([][]int, n+1)
	for i := range dp {
		dp[i] = make([]int, max_count+1)
	}

	for i := range phrases {
		s := phrases[i].s
		l := phrases[i].l
		p := phrases[i].p
		for j := range dp[i] {
			for k := s; k <= l; k++ {
				if 0 <= j-k {
					dp[i+1][j] = max(dp[i+1][j], max(dp[i][j], dp[i+1][j-k]+p))
				} else {
					dp[i+1][j] = max(dp[i+1][j], dp[i][j])
				}
			}
		}
	}

	fmt.Scan(&m)
	var invalid bool
	var points []int
	for i := 0; i < m; i++ {
		var m int
		fmt.Scan(&m)
		if dp[n][m] == 0 {
			invalid = true
		}
		points = append(points, dp[n][m])
	}

	if invalid {
		fmt.Println(-1)
	} else {
		for _, p := range points {
			fmt.Println(p)
		}
	}
}

C++: #include<iostream>
 
using namespace std;

#define MAX (39*39+1)

int max(int a,int b){ return (a>b)?a:b;}

int main(){
 
	int n;
	int s,l,p;
	int m;
	int w;
	int dp[MAX];
	bool f;

	cin>>n;

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

	dp[0]=0;

	for(int i=0;i<n;i++){
		cin>>s>>l>>p;
		f=true;
		while(f){
			f=false;
			for(int j=s;j<=l;j++){
				for(int k=0;k<MAX-j;k++){
					if(dp[k]>=0){
						if(dp[k+j]<dp[k]+p){
							dp[k+j]=dp[k]+p;
							f=true;
						}
					}
				}
			}
		}
	}

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

	return 0;
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); (i)--)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
using namespace std;

int main(){
    int n; cin >> n;
    int s[n], l[n], p[n]; rep(i, n) cin >> s[i] >> l[i] >> p[i];
    int m; cin >> m;
    int w[m]; rep(i, m) cin >> w[i];
    int dp[n+1][410];
    rep(i, n+1)rep(j, 400) dp[i][j] = -1;
    dp[0][0] = 0;
    rep(i, n)rep(j, 394)if(dp[i][j] >= 0){
        dp[i+1][j] = dp[i][j];
        For(k, s[i], l[i]+1) dp[i][min(j+k, 400)] = max(dp[i][min(j+k, 400)], dp[i][j] + p[i]);
    }
    int ans[m];
    rep(i, m){
        ans[i] = dp[n][w[i]];
        if(ans[i] == -1){
            cout << -1 << endl;
            return 0;
        }
    }
    rep(i, m) cout << ans[i] << endl;
}

C++: #include <iostream>
using namespace std;

int main(void)
{
	int n, s[393], l[393],p[393];
	cin >> n;
	for(int i = 0; i < n; i++) cin >> s[i] >> l[i] >> p[i];
	int m, w[393];
	cin >> m;
	int longest = 0;
	for(int i = 0; i < m; i++){
		cin >> w[i];
		longest = max(longest,w[i]);
	}
	
	int point[394][394] = {0};
	for(int j = 0; j < n; j++){
		for(int k = 0; k < longest; k++) point[j + 1][k] = point[j][k];

		for(int k = 0; k < longest; k++){
			if(k == 0 || point[j][k]){
				int count = 1;
				while(k + s[j] * count <= longest){
					for(int x = s[j]; x <= l[j]; x++){
						if(k + x <= longest){
							point[j + 1][k + x] = max(point[j + 1][k + x], point[j][k] + p[j]);
						}
					}
					count++;
				}
			}
		}
	}

	int max[393];
	for(int i = 0; i < m; i++){//i番目のメロディー
		if(point[n][w[i]]) max[i] = point[n][w[i]];
		else {max[0] = -1; break;}
	}


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

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
  int n, m, s[393], l[393], p[393], w;
  vector< int > dp( 394, -1); //dp[i]:i長のメロディを作るための最大値
  dp[0] = 0;
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> s[i] >> l[i] >> p[i];
  }
  for(int i = 0; i < n; i++){
    for(int j = 0; j >= 393; j++){
      if(dp[j] == -1) continue;
      for(int k = s[i]; k <= l[i] && j + k < 394; k++){
        dp[j + k] = max( dp[j + k], dp[j] + p[i]);
      }
    }
  }
  cin >> m;
  vector< int > ret(m);
  bool flag = true;
  for(int i = 0; i < m && flag; i++){
    cin >> w;
    ret[i] = dp[w];
    flag &= ret[i] != -1;
  }
  if(flag){
    for(int i = 0; i < m; i++){
      cout << ret[i] << endl;
    }
  }else{
    cout << -1 << endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;

int main(){
	int n,dp[400][400] = {};
	cin >> n;
	for(int i = 0;i < n;i++){
		int s,l,p;
		cin >> s >> l >> p;
		for(int j = 0;j < 400;j++){
			dp[i + 1][j] = max(dp[i + 1][j],dp[i][j]);
			for(int k = s;k <= l;k++) {
				int now = j + k;
				for(int l = 1;now < 400;l++,now += k){
					dp[i + 1][now] = max(dp[i + 1][now],dp[i][j] + p * l);
				}
			}
		}
	}
	int m,ans[400];
	cin >> m;
	for(int i = 0;i < m;i++){
		int w;
		cin >> w;
		ans[i] = dp[n][w];
		if(!ans[i]){
			cout << -1 << endl;
			return 0;
		}
	}
	for(int i = 0;i < m;i++) cout << ans[i] << endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = (a); i < (n); i++)
#define MP make_pair
#define all(s) (s).begin(), (s).end()
#define rev(s) (s).rbegin(), (s).rend()
#define X first
#define Y second

using namespace std;

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

int main(){
	//freopen("test.txt", "r", stdin);
	int n;
	cin >> n;

	vector<pair<int, P> > v(n);
	rep(i, n){
		cin >> v[i].Y.X >> v[i].Y.Y >> v[i].X;
	}
	sort(all(v));

	vi dp(400, -1);
	rep(i, n){
		for(int w = v[i].Y.X; w <= v[i].Y.Y; w++) dp[w] = v[i].X;
	}

	rep(w, 400){
		for(int i = w/2; i < w; i++){
			if(dp[i] == -1 || dp[w-i] == -1) continue;
			dp[w] = max(dp[w], dp[w-i]+dp[i]);
		}
	}

	int m;
	cin >> m;
	vi res;
	rep(i, m){
		int w;
		cin >> w;
		if(dp[w] != -1) res.push_back(dp[w]);
	}

	if(res.size() != m){
		cout << -1 << endl;
	}else{
		rep(i, m){
			cout << res[i] << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <fstream>

using namespace std;

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


int N,M;
int SS[400];
int LS[400];
int PS[400];
int WS[400];
int dp[401];

int main(){

  cin>>N;
  for(int i=0;i<N;i++){
    cin>>SS[i]>>LS[i]>>PS[i];
  }
  cin>>M;
  for(int i=0;i<M;i++)
    cin>>WS[i];
  for(int j=0;j<=400;j++){
    int res=-1;
    if(j==0)res=0;
    else{
      for(int k=0;k<N;k++){
        for(int l=SS[k];l<=LS[k];l++){
          if(j-l>=0){
            if(dp[j-l]!=-1){
              res=max(res,dp[j-l]+PS[k]);
            }
          }
        }
      }
    }
    dp[j]=res;
  }
  vector<int> res;
  for(int i=0;i<M;i++){
    if(dp[WS[i]]==-1){
      cout<<-1<<endl;
      return 0;
    }
    else{
      res.push_back(dp[WS[i]]);
    }
  }
  for(int i=0;i<(int)res.size();i++){
    cout<<res[i]<<endl;
  }
  
  return 0;
}