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>
#include <vector>
#include <algorithm>
using namespace std;

int n, m, s[400], l[400], p[400];
// dp[i][j] := i番目までのメロディを使っていて長さがjのときの最高点数
int dp[400][400];

int main(){
	for(int i = 0; i < 400; i++){
		for(int j = 0; j < 400; j++){
			dp[i][j] = -1;
		}
	}
	
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> s[i] >> l[i] >> p[i];
	}
	
	dp[0][0] = 0;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < 400; j++){
			if( dp[i][j] == -1 ) continue;
			
			dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
			for(int k = j + s[i]; k <= j + l[i]; k++){
				if( 400 <= k ) continue;
				dp[i][k] = max(dp[i][k], dp[i][j] + p[i]);
				dp[i+1][k] = max(dp[i+1][k], dp[i][j] + p[i]);
			}
		}
	}
	
	vector<int> ans;
	bool flag = false;
	cin >> m;
	for(int i = 0; i < m; i++){
		int w;
		cin >> w;
		if( dp[n][w] == -1 ){
			flag = true;
		}else{
			ans.push_back(dp[n][w]);
		}
	}
	if( flag ){
		cout << -1 << endl;
	}else{
		for(int i = 0; i < ans.size(); i++){
			cout << ans[i] << endl;
		}
	}
}

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

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

using ll = long long;
using pii  = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main(){
    int n;
    cin >> n;
    vi s(n), l(n), p(n);
    rep(i, n) cin >> s[i] >> l[i] >> p[i];
    int m;
    cin >> m;
    vi w(m);
    rep(i, m) cin >> w[i];
    int wsize = 0;
    rep(i, m) wsize = max(wsize, w[i]);
    vector<vi> dp(n+1, vi(wsize+1));
    rep(i, n) rep(j, wsize+1) {
        dp[i+1][j] = dp[i][j];
        if (j-s[i]>=0) {
            for (int k=s[i]; k<=l[i]&&j-k>=0; ++k) {
                dp[i+1][j] = max(dp[i+1][j], dp[i+1][j-k]+p[i]);
            }
        }
    }
    bool ok = true;
    vi ans(m);
    rep(i, m) {
        if (dp[n][w[i]] == 0) ok = false;
        else ans[i] = dp[n][w[i]];
    }
    if (ok) rep(i, m) cout << ans[i] << endl;
    else cout << -1 << endl;
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#define N 394
using namespace std;

int main(){
  int n,s[N],l[N],p[N],dp[N][N],m,w,ans[N],flag=0;
  cin>>n;
  for(int i=1;i<=n;i++) cin>>s[i]>>l[i]>>p[i];
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++) dp[i][j]=-1;
  dp[0][0]=0;
  for(int i=1;i<=n;i++){
    for(int j=0;j<N;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-1][j-k]!=-1) dp[i][j]=max(dp[i][j],dp[i-1][j-k]+p[i]);
      }
    }
  }
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    if(dp[n][w]==-1){
      flag=1;
      break;
    }else ans[i]=dp[n][w];
  }
  if(!flag)
    for(int i=0;i<m;i++) cout<<ans[i]<<endl;
  else cout<<-1<<endl;
  return 0;
}

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

const int MAXN = 400;
const int MAXW = 400;
int N;
int S[MAXN], L[MAXN], P[MAXN];
int dp[MAXW];

int main() {
  while(cin >> N) {
    for(int i = 0; i < N; ++i) {
      cin >> S[i] >> L[i] >> P[i];
    }
    fill(dp, dp+MAXW, -1);
    dp[0] = 0;

    for(int w = 0; w < MAXW; ++w) {
      if(dp[w] == -1) continue;
      for(int i = 0; i < N; ++i) {
        for(int j = S[i]; j <= L[i] && w+j < MAXW; ++j) {
          dp[w+j] = max(dp[w+j], dp[w] + P[i]);
        }
      }
    }

    int m;
    cin >> m;
    bool flag = true;
    int ans[m];
    for(int i = 0; i < m; ++i) {
      int w;
      cin >> w;
      if(dp[w] == -1) flag = false;
      ans[i] = dp[w];
    }
    if(flag) {
      for(int i = 0; i < m; ++i) {
        cout << ans[i] << endl;
      }
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#define N 394
using namespace std;

int main(){
  int n,s[N],l[N],p[N],dp[N][N],m,w,ans[N],flag=0;
  cin>>n;
  for(int i=1;i<=n;i++) cin>>s[i]>>l[i]>>p[i];
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++) dp[i][j]=-1;
  dp[0][0]=0;
  for(int i=1;i<=n;i++){
    for(int j=0;j<N;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-1][j-k]!=-1) dp[i][j]=max(dp[i][j],dp[i-1][j-k]+p[i]);
      }
      dp[i-1][j]=max(dp[i-1][j],dp[i][j]);
    }
  }
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    if(dp[n][w]==-1){
      flag=1;
      break;
    }else ans[i]=dp[n][w];
  }
  if(!flag)
    for(int i=0;i<m;i++) cout<<ans[i]<<endl;
  else cout<<-1<<endl;
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;

int n, s[393], l[393], p[393], m, w[393], dp[394];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d%d%d", &s[i], &l[i], &p[i]);
  for (int i = 1; i <= 393; i++) dp[i] = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 393; j++) {
      for (int k = s[i]; k <= l[i]; k++) if (j - k >= 0 && dp[j - k] != -1) dp[j] = max(dp[j], dp[j - k] + p[i]);
    }
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &w[i]);
    if (dp[w[i]] == -1) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < m; i++) {
    printf("%d\n", dp[w[i]]);
  }
}