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

import (
	"fmt"
)

type phrase struct {
	s, l, p int
}

const melody_len int = 393

var n, m 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, melody_len+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>
using namespace std;

struct data{
  int s,l,p;
  data(int s=0, int l=0, int p=0):s(s),l(l),p(p){}
};

int n,dp[2][394];
data v[393];

int solve(int w){
  fill(dp[0],dp[2],0);

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

  return dp[1][w];
}

int main(){
  while(cin >> n){
    for(int i=0;i<n;i++) cin >> v[i].s >> v[i].l >> v[i].p;
    int m,w,ans[393];
    bool f = false;
    cin >> m;
    for(int i=0;i<m;i++){
      cin >> w;
      if(f) continue;
      ans[i] = solve(w);
      if(ans[i] == 0) f = true;
    }
    if(f) cout << -1 << endl;
    else for(int i=0;i<m;i++) cout << ans[i] << endl;
  }
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

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];
	}
	vi ans(m);
	bool flag=false;
	REP(melody,m){
		vvi dp(n+1,vi(w[melody]+1));
		REP(i,n){
			REP(j,w[melody]+1){
				FOR(k,s[i],l[i]+1){
					if(j-k>=0){
						dp[i+1][j]=max(dp[i+1][j],dp[i][j-k]+p[i]);
					}
					dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
				}
			}
		}
		ans[melody]=dp[n][w[melody]];
		if(ans[melody]==0){
			flag=true;
		}
	}
	if(flag){
		cout<<-1<<endl;
	}else{
		REP(i,m){
			cout<<ans[i]<<endl;
		}
	}

}

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

#define jougen 393
int dp[jougen+1][jougen+1];

int main(){
    int n;
    cin >> n;
    int s[n+1],l[n+1],p[n+1];
    int i,j,k;
    for(i=1; i<=n; i++){
        cin >> s[i] >> l[i] >> p[i];
    }
    for(j=0; j<=jougen; j++){
        for(k=0; k<=jougen; k++){
            dp[j][k]=0;
        }
    }
    for(i=1; i<=n; i++){
        for(j=1; j<=jougen; j++){
            for(k=0; k<=jougen; k++){
                if(j-s[i]<0){
                    dp[j][k]=dp[j][k];
                }else if(k-l[i]<0){
                    dp[j][k]=max(dp[j][k],dp[j-s[i]][0]+p[i]);
                }else{
                    dp[j][k]=max(dp[j][k],dp[j-s[i]][k-l[i]]+p[i]);
                }
            }
        }
    }
    int m;
    cin >> m;
    bool nonzero=1;
    int w[m+1];
    for(i=1; i<=m; i++){
        cin >> w[i];
        if(dp[w[i]][w[i]]<=0){
            nonzero=0;
        }
    }
    if(nonzero==1){
        for(i=1; i<=m; i++){
            cout << dp[w[i]][w[i]] << endl;
        }
    }else{
        cout << -1 << endl;
    }
    return 0;
}

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

int n,s,l,p,m,w;
int dp[400][400];
int ans[400];

int main(){
  cin >> n;
  memset(dp,-1,sizeof(dp));
  dp[0][0] = 0;

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

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

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

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);++(i))
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);++(i))
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);--(i))
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);--(i))
#define each(i, a) for(auto &i : a)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
#define out(y,x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x))
#define line cout << "------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
//#define stop system("pause") //comment out this on AOJ.
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10; //1e-9?
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T>inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& elemnt : v)is >> elemnt; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }

using t3 = tuple<int, int, int>;
int main() {
    init();

    int n; cin >> n;
    VI s(n), l(n), p(n);
    FOR(i, 0, n)cin >> s[i] >> l[i] >> p[i];

    auto dp = defvec<int>(-1, n + 1, 394);
    eFOR(i, 0, n)dp[i][0] = 0;
    FOR(i, 0, n)eFOR(j, 0, 393) {
        eFOR(k, s[i], l[i]) {
            if (k <= j && dp[i + 1][j - k] != -1) {
                chmax(dp[i + 1][j], dp[i + 1][j - k] + p[i]);
            }
        }
        chmax(dp[i + 1][j], dp[i][j]);
    }

    int m; cin >> m;
    VI ans;
    FOR(q, 0, m) {
        int w; cin >> w;
        if (0 < dp[n][w])ans.push_back(dp[n][w]);
    }

    if (ans.size() < m)cout << "-1\n";
    else FOR(i, 0, m)cout << ans[i] << "\n";
}