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

class dp{
public:
  int n;
  static const int limit=393;
  int s[limit+1], l[limit+1], p[limit+1];
  int d[limit+1][limit+1];
  bool can[limit+1][limit+1];

  dp(int n):n(n){}

  void init(){
    for(int i=0; i<=n; i++){
      for(int j=0; j<=limit; j++) {
        d[i][j] = 0;
        if(j==0) can[i][j] = true;
        else can[i][j] = false;
      }
    }
    for(int i=1; i<=n; i++){
      for(int j=0; j<=limit; j++){
        int temp=0;
        for(int k=s[i]; k<=l[i]; k++){
          if(can[i-1][j]){
            temp = max(temp, d[i-1][j]);
            can[i][j] = true;
          }
          if(j>=k) {
            if(can[i][j-k]){
              temp = max(temp, d[i][j-k]+p[i]);
              can[i][j] = true;
            }
          }
        }
        d[i][j] = temp;
      }
    }
  }

  vector<int> ans(vector<int> w){
    vector<int> ans;
    for(int i=0; i<w.size(); i++){
      if(!can[n][w[i]]){
        ans = vector<int>(1, -1);
        return ans;
      }
      ans.push_back(d[n][w[i]]);
    }
    return ans;
  }
};

int main(){
  int n; cin >> n;
  dp d(n);

  int s, l, p;
  for(int i=1; i<=n; i++){
    cin >> s >> l >> p;
    d.s[i] = s;
    d.l[i] = l;
    d.p[i] = p;
  }

  d.init();

  int m; cin >> m;
  vector<int> melodies;
  for(int i=0; i<m; i++){
    int w; cin >> w;
    melodies.push_back(w);
  }

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

  return 0;
}

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define ull unsigned long long
#define ld long double

template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
template <class T>
using VVV = V<V<V<T>>>;

#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) rep2(i, 0, n)
#define rep1(i, n) rep1(i, 1, n + 1)

// #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 INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

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

const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

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() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(15);
    //    cout << std::setfill('0') << std::setw(2);

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

    int m;
    cin >> m;

    V<int> dp(401, 0);
    rep(i, n) {
        rep2(j, s[i], l[i] + 1) {
            rep(k, 400) {
                if (k + j > 400) break;
                dp[k + j] = max(dp[k + j], dp[k] + p[i]);
            }
        }
    }
    V<int> ans;
    rep(t, m) {
        int w;
        cin >> w;
        if (dp[w] == 0) {
            cout << -1 << endl;
            return 0;
        }
        ans.push_back(dp[w]);
    }

    for (auto a : ans) {
        cout << a << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#define REP(i,N) for(int i=0;i<N;++i)

using namespace std;

typedef pair<int, int> P;

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

	int dp[400];
	REP(i, 400) dp[i] = -1;

	REP(i, N) {
		int s, g, p;
		cin >> s >> g >> p;

		for(int j = s; j <= g; ++j) {
			dp[j] = max(dp[j], p);
		}
	}
	
	REP(i, 400) REP(j, i+1) {
		if ( dp[i] != -1 && dp[j] != -1 && (i + j < 400) )
			dp[i+j] = max(dp[i+j], dp[i]+dp[j]);
	}

	int M;
	cin >> M;
	
	vector<int> ans;
	REP(i, M) {
		int q;
		cin >> q;

		if ( dp[q] != -1 ) {
			ans.push_back(dp[q]);
		}else {
			cout << -1 << endl;
			return 0;
		}
	}

	REP(i, ans.size()) {
		cout << ans[i] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

#define REP(i, n) for(int i=0;i<(int)n;i++)
#define rep(i, n) for(int i=0;i<(int)n;i++)
#define reep(i, s, t) for(int i=s;i<(int)t;i++)
#define irep(i, n) for(int i=(int)n-1;i>=0;i--)
#define ireep(i, n, s) for(int i=(int)n-1;i>=s;i--)
#define all(v) v.begin(), v.end()
#define vc vector
#define pb push_back

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

int n,m;
const int INF = 1<<25;
int dp[1005];
int main(){
	cin >> n;
	REP(i, 400) dp[i] = -INF;
	dp[0] = 0;
	REP(tttt, n){
		int s, t, w;
		cin >> s >> t >> w;
		irep(i, 400){
			for(int j=s;j<=t;j++) dp[i+j] = max(dp[i+j], dp[i] + w);
		}
	}
	vi ans;
	int f = 1;
	cin >> m;
	REP(i, m){
		int x;
		cin >> x;
		ans.pb(dp[x]);
		if(dp[x] < 0) f = 0;
	}
	if(!f) cout << -1 << endl;
	else{
		for(int v : ans) cout << v << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[401][401][401]={};
int main(){
  int n;
  cin>>n;
  int s[401],l[401],p[401];
  
  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<400;j++){
      for(int k=0;k<400;k++){
	int ny=min(s[i]+j,399);
	int nz=min(l[i]-s[i]+k,399);
	
	/*
	  if(i&&j-s[i-1]>=0&&k-(l[i-1]-s[i-1])>=0)
	  if(dp[i][j-s[i-1]][k-(l[i-1]-s[i-1])])
	    dp[i][j][k]=max(dp[i][j][k],dp[i][j-s[i-1]][k-(l[i-1]-s[i-1])]+p[i-1]);
	*/
	
	
	if(j+k==0)dp[i+1][ny][nz]=max(dp[i+1][ny][nz],p[i]);
	
	dp[i+1][j][k]=max(dp[i+1][j][k],dp[i][j][k]);
	
	if(dp[i][j][k]){
	  dp[i+1][ny][nz]=max(dp[i+1][ny][nz],dp[i][j][k]+p[i]);
	  dp[i][ny][nz]=max(dp[i][ny][nz],dp[i][j][k]+p[i]);
	}
      }
    }
  }
  int m,a[400],f=0;
  cin>>m;
  for(int i=0;i<m;i++){
    int w;
    cin>>w;
    int ans=0;
    for(int j=0;j<400;j++){
      for(int k=0;k<400;k++){
	if(j<=w&&j+k>=w)ans=max(ans,dp[n][j][k]);
      }
    }
    if(ans==0)f=1;
    a[i]=ans;
  }
  if(f)cout<<-1<<endl;
  else for(int i=0;i<m;i++)cout<<a[i]<<endl;
  return 0;
}