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;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using vs=vector<string>;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF    	((ll)1ll<<60)
#define INF     	((int)1<<30)
#define EPS     	(1e-9)
#define MOD     	(1000000007ll)
#define fcout(a)	cout<<setprecision(a)<<fixed
#define fs 			first
#define sc			second
#define PI			3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s; a=0;} return os;}
template<class T> void chmax(T &a, const T &b){if(a<b)a=b;}
template<class T> void chmin(T &a, const T &b){if(a>b)a=b;}
void YN(bool b){cout<<(b?"YES":"NO")<<endl;}
void Yn(bool b){cout<<(b?"Yes":"No")<<endl;}
void yn(bool b){cout<<(b?"yes":"no")<<endl;}

ll dp[394];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,m,w;
	cin>>n;
	memset(dp,-1,sizeof(dp));
	vl s(n),l(n),p(n);
	rep(i,n)cin>>s[i]>>l[i]>>p[i];
	dp[0]=0;
	rep(i,n){
		rep(j,394){
			for(int k=s[i];k<=l[i];k++){
				if(j-k>=0&&dp[j-k]!=-1)chmax(dp[j],dp[j-k]+p[i]);
			}
		}
	}
	cin>>m;
	vi ans;
	rep(i,m){
		cin>>w;
		if(dp[w]==-1){cout<<"-1"<<endl;return 0;}
		ans.push_back(dp[w]);
	}
	rep(i,ans.size()){
		cout<<ans[i]<<"\n";
	}
}

C++: #include <iostream>
using namespace std;
int n, q, w, f, l[399], r[399], p[399], s[399], dp[399];
int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> l[i] >> r[i] >> p[i];
	for(int i = 1; i <= 393; i++) {
		dp[i] = -999999999;
		for(int j = 0; j < n; j++) {
			for(int k = l[j]; k <= r[j] && i - k >= 0; k++) dp[i] = max(dp[i], dp[i - k] + p[j]);
		}
	}
	cin >> q;
	for(int i = 0; i < q; i++) cin >> w, s[i] = dp[w], f = f | (dp[w] < 0 ? 1 : 0);
	if(f) cout << -1 << endl;
	else for(int i = 0; i < q; i++) cout << s[i] << endl;
}

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

int main(){
	int n,dp[400][400] = {};
	cin >> n;
	for(int i = 0;i < n;i++){
		int s,l,p;
		cin >> s >> l >> p;
		for(int j = 0;j < 400;j++){
			dp[i + 1][j] = max(dp[i + 1][j],dp[i][j]);
			for(int k = s;k <= l;k++) {
				if(j + k < 400) dp[i + 1][j + k] = max(dp[i + 1][j + k],dp[i][j] + p);
			}
		}
	}
	int m,ans[400];
	cin >> m;
	for(int i = 0;i < m;i++){
		int w;
		cin >> w;
		ans[i] = dp[n][w];
		if(!ans[i]){
			cout << -1 << endl;
			return 0;
		}
	}
	for(int i = 0;i < m;i++) 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];
  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) cout << dp[w] << endl;
	else cout << -1 << endl;
  }
}

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