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++: // clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

main() {
  int n = in();
  vector<int> s(n), l(n), p(n);
  rep(i, n) cin >> s[i] >> l[i] >> p[i];
  auto dp = vectors(n + 1, 394, 0LL);
  loop(i, 1, n + 1) rep(j, 394) {
    dp[i][j] = dp[i - 1][j];
    loop(k, s[i - 1], l[i - 1] + 1) {
      if (j - k >= 0) cmax(dp[i][j], dp[i][j - k] + p[i - 1]);
    }
  }
  int m = in();
  vector<int> ans;
  ans.reserve(m);
  while (m--) {
    int w = in();
    if (dp[n][w] == 0) {
      cout << -1 << endl;
      return 0;
    }
    ans.emplace_back(dp[n][w]);
  }
  for (auto &x : ans) cout << x << endl;
}

C++: #include<iostream>
#include<set>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class Phrase {
    public:
    int s,l,p;
    Phrase(int s,int l,int p) {

        this->s = s;
        this->l = l;
        this->p = p;
    }
    Phrase() {
        s=l=p=0;
    }

};

int n;
Phrase phra[400];
int m;
int w[400];
int dp[400];

int main() {

    cin>>n;
    rep(i,n) {
        int s,l,p;
        cin>>s>>l>>p;
        phra[i] = Phrase(s,l,p);
    }
    cin>>m;
    rep(i,m) cin>>w[i];
    rep(i,400) dp[i]=-1000000;
    dp[0]=0;

    for(int k=0;k<400;k++) {
        rep(i,n) {
            for(int j=phra[i].s;j<=phra[i].l;j++) {
                dp[k+j] = max(dp[k+j],dp[k]+phra[i].p);
            }
        }
    }

    multiset<int> ans;
    bool flag = false;
    
    rep(i,m) {
        int t = dp[w[i]];
        if(t < 0) flag=true;
        ans.insert(dp[w[i]]);
    }
    if(flag || ans.find(-1) != ans.end()) {
        cout<<"-1"<<endl;
        return 0;
    }

    multiset<int>::iterator it = ans.begin();
    while(it != ans.end()) {
        cout<<(*it)<<endl;
        it++;
    }

    return 0;
}

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

int n;
int S[393], L[393], P[393];
int m;
int W[393];
int dp[394][394];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> S[i] >> L[i] >> P[i];
    }

  	memset(dp, 0, sizeof(dp));
  
    for (int i = 0; i < n; i++){
        for (int j = 1; j <= 393; j++){
          	dp[i+1][j] = dp[i][j];
            for (int k = S[i]; k <= L[i] && k <= j; k++){
                dp[i+1][j] = max({dp[i+1][j], dp[i+1][j-k]+P[i], dp[i][j-k]+P[i]});    	
            }
        }
    }


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

    return 0;
}

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;
int n, w, dp[1000000][393+1];
vector<int>p, len;

int rec(int i, int j){
  if(dp[i][j] >= 0) return dp[i][j];
  int res;
  if(i == p.size() || j == 0) res = 0;
  else if(j < len[i]) res = rec(i + 1, j);
  else  res = max(rec(i + 1, j), rec(i + 1, j - len[i]) + p[i]);

  return dp[i][j] = res;
}

int main(){
  int n, m, s, l, point;
  memset(dp, -1, sizeof(dp));
  scanf("%d", &n);
  rep(i, n){
    scanf("%d%d%d", &s, &l, &point);
    for(int j = s; j <= l; j++){ len.push_back(j); p.push_back(point);}
  }
  scanf("%d", &m);
  vector<int>res;
  rep(i, m){
    scanf("%d", &w);
    res.push_back(rec(0, w));
  }
  bool f = true;
  rep(i, res.size()) if(res[i] == 0) f = false;
  if(f) rep(i, res.size()) printf("%d\n", res[i]);
  else puts("-1");
  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]=dp[i-1][j];
      for(int k=s[i];k<=l[i];k++){
	if(j-k>=0&&dp[i][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;
}