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;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
const int mod = 1000000007;
const int max_n = 200005;
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;
}
int main(){
    ll n;cin >> n;
    vl phe(400,0);
    rep(i,n){
        ll l,r,s;cin >> l >>r >>s;
        repl(j,l,r+1){
            chmax(phe[j],s);
        }
    }
    ll m;cin >> m;
    vl ans;bool check=false;
    rep(i,m){
        ll p;cin >> p;
        vl dp(p+1,0);
        rep(j,400){
            rep(k,p+1){
                if(k+j<=p)chmax(dp[k+j],dp[k]+phe[j]);
            }   
            //if(j<=10){rep(k,p+1)cout << dp[k] <<" ";cout <<endl;}
        }
        ans.pb(dp[p]);
        if(dp[p]==0)check=true;
    }
    if(check)cout << -1 <<endl;
    else rep(i,m)cout << ans[i] <<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];

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;
    
    rep(i,m) ans.insert(dp[w[i]]);    
    if(ans.find(0) != ans.end()) {
        cout<<"-1"<<endl;
        return 0;
    }

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




    return 0;
}
tt

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(j-s[i]>=0&&k-(l[i]-s[i])>=0)
	  if(dp[i][j-s[i]][k-(l[i]-s[i])])
	    dp[i][j][k]=max(dp[i][j][k],dp[i][j-s[i]][k-(l[i]-s[i])]+p[i]);
	
	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]);
      }
    }
  }
  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;
}

C++: #include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
    return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main() {
    constexpr int N = 393;
    int n; cin >> n;
    vector<int> s(n), l(n), p(n);
    REP(i, n) cin >> s[i] >> l[i] >> p[i];

    auto dp = make_v(n + 1, N + 1, 0);

    REP(i, n)REP(j, N + 1) {
        FOR(k, s[i], l[i] + 1) {
            if (j + k <= N) {
                dp[i][j + k] = max(dp[i][j + k], dp[i][j] + p[i]);
            }

            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        }
    }

    int m; cin >> m;
    vector<int> ans(m);
    REP(i, m) {
        int w; cin >> w;
        ans[i] = dp[n][w];
    }

    if (find(ans.begin(), ans.end(), 0) != ans.end()) {
        cout << -1 << endl;
    } else {
        REP(i, m) cout << ans[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
//#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
//#include <stack>
//#include <limits>
#include <sstream>
//#include <functional>
using namespace std;

#define len(array)  (sizeof (array) / sizeof *(array))
#define rep(i, s, e) for(int i = s;i < e;i++)
#define rrep(i, e, s) for(int i = e;s <= i;i--)
#define vrange(v) v.begin(), v.end()
#define vrrange(v) v.rbegin(), v.rend()
#define vsort(v) sort(vrange(v))
#define vrsort(v) sort(vrrange(v))
#define arange(a) a, a + len(a)
#define asort(a) sort(arange(a))
#define arsort(a, t) sort(arange(a), greater<t>())
#define afill(a, v) fill(arange(a), v)
#define afill2(a, v, t) fill((t *)a, (t *)(a + len(a)), v)
#define fmax(a, b) (a < b? b : a)
#define fmin(a, b) (a > b? b : a)
#define fabs(a) (a < 0? -a : a)
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-10;
//const int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
//const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
//const int weight[] = {0,1,10,100,1000,10000,100000,1000000,10000000};
//priority_queue< int, vector<int>, greater<int> > q;

void doIt(){
  int n, m, s, l, p, w, dp[394], ans[394];
  bool bImpossible = false;
  cin >> n;
  afill(dp, -INF);
  rep(i, 0, n){
	cin >> s >> l >> p;
	rep(j, s, l + 1){
	  dp[j] = max(dp[j], p);
	}
  }
  rep(i, 1, 394){
	rep(j, 1, i){
	  if(dp[j] != -INF && dp[i - j] != -INF) dp[i] = max(dp[i], dp[j] + dp[i - j]);
	}
  }
  cin >> m;
  rep(i, 0, m){
	cin >> w;
	if(dp[w] != -INF) ans[i] =  dp[w];
	else ans[i] = -1, bImpossible = true;
  }
  if(bImpossible) cout << -1 << endl;
  else{
	rep(i, 0, m){
	  cout << ans[i] << endl;
	}
  }
}

int main() {
  doIt();
  return 0;
}