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 <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
template<class T> inline bool amax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool amin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> ostream& operator << (ostream &os, const vector<T> &v) { os << "["; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) { os << (it != v.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T> &s) { os << "["; for (typename set<T>::const_iterator it = s.begin(); it != s.end(); it++) { os << (it != s.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class Key, class Val> ostream& operator << (ostream &os, const map<Key, Val> &m) { os << "{"; for (typename map<Key, Val>::const_iterator it = m.begin(); it != m.end(); it++) { os << (it != m.begin() ? ", " : "") << it->first << ":" << it->second; } os << "}"; return os; }
template<class T, class S> ostream& operator << (ostream &os, const pair<T, S> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <class T> istream& operator >> (istream &is, vector<T> &v) { for (size_t i = 0; i < v.size(); i++) is >> v[i]; return is; }
template <class Target, class Source> inline Target lexical_cast (const Source &s) { Target t; stringstream ss; ss << s; ss >> t; return t; }

//> v < ^ (clock wise)
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
const int INFI = 1<<28;
const long long int INFL = 1LL<<60;
const double INFD = 1e+300;
const float INFF = 1e+100;
const double EPS = 1e-8;
const long long int MOD = 1000000007;

int solve ();
int main () {
    cout.setf(ios::fixed); cout.precision(10);
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}

int solve () {
    int n;
    cin >> n;
    int s, l, p;
    int score[1000];
    for (int i = 0; i < 1000; i++) score[i] = -INFI;
    score[0] = 0;
    for (int i = 0; i < n; i++) {
        cin >> s >> l >> p;
        for (int k = 1000-1; k >= 0; k--) {
            for (int j = l; j >= s; j--) {
                if (k+j < 1000) score[k+j] = max(score[k+j], score[k]+p);
            }
        }
    }

    int m;
    cin >> m;
    vector<int> ans(m);
    bool ng = false;
    for (int i = 0; i < m; i++) {
        int w;
        cin >> w;
        ans[i] = score[w];
        ng |= score[w]<0;
    }
    if (ng) cout << -1 << endl;
    else {
        for (int i = 0; i < m; i++) cout << ans[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll LongINF=1e13+7;
const int INF=1e9+7;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
void clear(queue<pair<int, int>> &q){queue<pair<int, int>> empty;swap(q, empty);} //queueの中身の型は適時変更を忘れない

using vi=vector<int>;
using vii=vector<vi>;

#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define rv reverse
#define ALL(a) (a).begin(),(a).end()
#define decimal(x) fixed<<setprecision(x)


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];
    vii dp(N+1,vi(400,-1));
    rep(i,N)dp[i][0]=0;

    rep(i,N){
        rep(j,400){
            chmax(dp[i+1][j],dp[i][j]);
            REP(k,s[i],l[i]+1){
                if(j-k>=0)chmax(dp[i+1][j],dp[i+1][j-k]+p[i]);
            }
            //cout<<dp[i+1][j]<<" ";
        }
        //cout<<endl;
    }
    vi ANS;
    for(auto x:w){
        if(dp[N][x]==-1){
            cout<<-1<<endl;
            return 0;
        }else ANS.pb(dp[N][x]);
    }
    for(auto x:ANS)cout<<x<<endl;
    return 0;
}

//a=97,z=122

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

int main(){
  int n,m,w,s,l,p;
  int dp[400],fr[400],ans[400];
  
  cin >> n;
  for(int i=1;i<=393;i++)fr[i] = 0;

  for(int i=0;i<n;i++){
    cin >> s >> l >> p;
    for(int j=s;j<=l;j++)fr[j] = max(fr[j],p);
  }

  dp[0] = 0; for(int i=1;i<=393;i++)dp[i] = -1;

  for(int i=1;i<=393;i++){
    if(fr[i]){
      for(int j=0;j<=393;j++){
	if(dp[j]>=0)dp[i+j] = max(dp[i+j],dp[j]+fr[i]);
      }
    }
  }

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

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