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 <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int> >
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; }
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

  int n;
  cin >> n;
  int s[n], l[n], p[n];
  rep(i, 0, n) {
    cin >> s[i] >> l[i] >> p[i];
  }

  ll dp[400];
  memset(dp, 0, sizeof(dp));
  rep(i, 0, n) {
    rep(j, 0, 400) {
      rep(k, s[i], l[i] + 1) {
        if(j - k >= 0) {
          chmax(dp[j], dp[j - k] + p[i]);
        }
      }
    }
  }

  int m;
  cin >> m;
  vector<int> rsl;
  rep(i, 0, m) {
    int w;
    cin >> w;
    if(dp[w] == 0) {
      cout << -1 << endl;
      return 0;
    }
    rsl.push_back(dp[w]);
  }

  rep(i, 0, rsl.size()) {
    cout << rsl[i] << endl;
  }
}

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

using namespace std;

#define ALL(c) c.begin(),c.end()
#define RALL(c) c.rbegin(),c.rend()
#define REP(type,i,n) for(type i=0;i<(n);++i)
#define MP(a,b) make_pair((a),(b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int,int> Pi;

const int INF=100000000;
const long long int INF_=1000000000000000000;

int main(){
	int N;
	cin >> N;
	vector<int> m(394);
	REP(int,i,N){
		int s,l,p;
		cin >> s >> l >> p;
		for(int j=s; j<=l; ++j) m[j]=max(m[j],p);
	}
	REP(int,i,394){
		REP(int,j,i+1){
			m[i]=max(m[i],m[i-j]+m[j]);
		}
	}
	//REP(int,i,9) cout << m[i] << ' ';
	cout << endl;
	int M;
	cin >> M;
	vector<int> ans;
	bool flag=true;
	REP(int,i,M){
		int w;
		cin >> w;
		if(m[w]==0) flag=false;
		else ans.push_back(m[w]);
	}
	if(!flag) cout << -1;
	else REP(int,i,ans.size()){
		cout << ans[i];
		if(i!=ans.size()-1) cout << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int main(){
	int n,m;
	cin >> n;
	int s,l,p;
	int w[400]={0,};
	for(int i=0;i<n;i++){
		cin >> s >> l >> p;
		for(int j=s;j<=l;j++){
			for(int k=0;k+j<=399;k++){
				w[k+j]=max(w[k+j],w[k]+p);
			}
		}
	}
	cin >> m;
	int d[m];
	bool ans = true;
	for(int i=0;i<m;i++){
		cin >> d[i];
		if(!w[d[i]])ans=false;
	}
	if(!ans)cout << -1 << endl;
	else{
		for(int i=0;i<m;i++){
			cout << w[d[i]] << endl;
		}
	}
}

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

#define MAX (400*400+1)

int max(int a,int b){ return (a>b)?a:b;}

int main(){
 
	int n;
	int s,l,p;
	int m;
	int w;
	int dp[2][MAX];

	cin>>n;

	for(int i=0;i<MAX;i++){
		for(int j=0;j<2;j++){
			dp[j][i]=-1;
		}
	}

	dp[0][0]=0;

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

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

	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int main() {
	int n; cin >> n;
	int dp[500] = {0};
	for (int i = 0; i < n; ++i) {
		int s, l, p; cin >> s >> l >> p;
		for (int w = s; w <= l; ++w) {
			for (int from = 0; from < 500; ++from) {
				if (from + w < 500) {
					dp[from+w] = max(dp[from+w], dp[from]+p);
				}
			}
		}
	}
	int m; cin >> m;
	vector<int> v;
	bool f = true;
	for (int t = 0; t < m; ++t) {
		int w; cin >> w;
		if (dp[w] > 0) v.push_back(dp[w]);
		else f = false;
	}
	if (f) {
		for (int i = 0; i < v.size(); ++i) {
			cout << v[i] << endl;
		}
	}
	else {
		cout << -1 << endl;
	}
}

C++: #define REP(i, t, n)    for (int i = t; i < n; i++)
#define ALL(n)              (n).begin(),(n).end()
#include <limits.h>
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <functional>
#include <algorithm>
#include <cstring>
#include <unordered_map>
 
using namespace std;
 
typedef bool             BOOL;
typedef short            WORD;
typedef unsigned short   U_WORD;
typedef long             DWORD;
typedef unsigned long    U_DWORD;
typedef long long        QWORD;
typedef unsigned long long U_QWORD;
 
 
 
#define M_MAX     (393)
#define N_MAX     (393)
#define LEN_MAX   (393)
typedef pair<DWORD, DWORD> PAIR;
 
int N, M;
DWORD Dp[N_MAX + 1][LEN_MAX + 1] = {};
DWORD adwMelody[M_MAX] = {};
DWORD adwPoint[N_MAX] = {};
vector<PAIR> vFraseLength;
 
 
void input()
{
    cin >> N;
    REP(i, 0, N) {
        PAIR tempLength;
        cin >> tempLength.first >> tempLength.second;
        vFraseLength.push_back(tempLength);
        cin >> adwPoint[i];
    }
 
    cin >> M;
    REP(i, 0, M) {
        cin >> adwMelody[i];
    }
 
}
 
void CalcDpTable()
{
    REP(i, 0, N) {
        PAIR nowFrase = vFraseLength[i];
        DWORD   nowPoint = adwPoint[i];
        REP(j, 0, LEN_MAX + 1) {
            if (j >= nowFrase.first) {
                DWORD tmpVal = Dp[i][j];
                REP(k, nowFrase.first, nowFrase.second+1) {
                    if (j < k) break;
                    tmpVal = max(tmpVal, max(Dp[i][j-k] + nowPoint, Dp[i+1][j-k] + nowPoint));
                }
                Dp[i+1][j] = tmpVal;
            } else {
                Dp[i + 1][j] = Dp[i][j];
            }
        }
    }
    return;
}
 
void makeAns()
{
    vector<DWORD> ans;
    REP(i, 0, M) {
        DWORD tmpAns = Dp[N][adwMelody[i]];
 
        if (tmpAns == 0) {
            cout << -1 << endl;
            return;
        } else {
            ans.push_back(tmpAns);
        }
    }
 
    REP(i, 0, M) {
        cout << ans[i] << endl;
    }
    return;
}
 
void solve_main()
{
    input();
 
    CalcDpTable();
 
    makeAns();
}
 
int main()      
{
#ifdef FOR_IDE
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif
 
    solve_main();
    return 0;
}