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 <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] << ' ';
	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 << endl;
	else REP(int,i,ans.size()){
		cout << ans[i] << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int s[400], l[400], p[400];
int dp[400][400];
int main() {
	int n; scanf("%d", &n);
	rep(i, n)scanf("%d%d%d", &s[i], &l[i], &p[i]);
	rep(i, n)rep(j, 400) {
		if (j && !dp[i][j])continue;
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
		for (int k = s[i]; k <= l[i] && j + k < 400; k++)
			dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i + 1][j] + p[i]);
	}
	int m; scanf("%d", &m);
	vector<int>v;
	rep(i, m) {
		int w; scanf("%d", &w);
		if (dp[n][w] == 0) { puts("-1"); return 0; }
		v.push_back(dp[n][w]);
	}
	for (int i : v)printf("%d\n", i);
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,s[400],l[400],p[400];
	int i,j,k;
	int m,w[400],dp[400],ans[400];
	int mx,flg,x;
	
	scanf("%d",&n);
	for(i=0;i<n;i++)	{
		scanf("%d %d %d",&s[i],&l[i],&p[i]);
	}
	scanf("%d",&m);
	mx=-1;
	for(i=0;i<m;i++)	{
		scanf("%d",&w[i]);
		if(mx<w[i])	mx=w[i];
	}
	for(i=0;i<=mx;i++)	{
		dp[i]=-1;
	}
	dp[0]=0;
	for(i=0;i<n;i++)	{
		for(j=0;j<mx;j++)	{
			if(dp[j]!=-1)	{
				for(k=s[i];k<=l[i];k++)	{
					if(j+k<=mx)	{
						dp[j+k]=max(dp[j+k],dp[j]+p[i]);
					}
				}
			}
		}
	}
	flg=1;
	for(i=0;i<m;i++)	{
		if(dp[w[i]]==-1)	{
			flg=0;	break;
		}
	}
	if(flg==0)	{
		printf("-1\n");
	}
	else {
		for(i=0;i<m;i++)	{
			printf("%d\n",dp[w[i]]);
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef vector<int> vi;

const int len = 393;
int main(){
  int n;
  cin >> n;
  vector<vi> dp(n+1,vi(len+1,-1));
  dp[0][0] = 0;

  rep(i,n){
    int s,l,p;
    cin >> s >> l >> p;
    rep(j,len){
      dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
      for(int t=s;t<=l;t++){
	if(j+t<=len){
	  dp[i+1][j+t] = max(dp[i+1][j+t], dp[i][j] + p);
	}
      }
    }
  }

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

  rep(i,m)cout << ans[i] << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}

bool compareg(Pi a, Pi b) {
    if(a.second != b.second){
        return a.second > b.second;
        
    }else{
        return a.first > b.first;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};



int main() {
    ll n;
    cin>>n;
    vector<ll> s(n);
    vector<ll> l(n);
    vector<ll> p(n);

    rep(i,n){
        cin>>s[i]>>l[i]>>p[i];
    }
    ll m;
    cin>>m;
    vector<ll> w(m);

    rep(i,m){
        cin>>w[i];
    }

    vector<vector<ll>> dp(n+1,vector<ll>(400));

    rep(i,n){
        rep(j,395){
            rep3(k,s[i],l[i]+1){
                if(j-k>=0){
                    dp[i+1][j] = max({dp[i+1][j],dp[i][j],dp[i][j-k]+p[i],dp[i+1][j-k]+p[i]});

                }else{
                    dp[i+1][j] = max({dp[i+1][j],dp[i][j]});
                }
            }
  
        }
    }
    vector<ll> ans(m);
    rep(i,m){
        if(dp[n][w[i]] == 0){
            cout<<-1<<endl;
            return 0;
        }else{
            ans[i] = dp[n][w[i]];
        }

    }
    
    rep(i,m){
        cout<<ans[i]<<endl;
    }
    return 0;
    

}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[999],a[999],l[999],r[999],m[999],N,Q,i,j,k,OK;
main(){
	cin>>N;
	for(i=0;i<N;i++)cin>>l[i]>>r[i]>>m[i];
	for(i=0;i<N;i++){
		for(j=0;j<400;j++){
			for(k=l[i];k<=r[i];k++){
				dp[j+k]=max(dp[j+k],dp[j]+m[i]);
			}
		}
	}
	cin>>Q;
	for(i=0;i<Q;i++){
		cin>>a[i]; 
		if(!dp[a[i]])OK=1;
	}
	if(OK)cout<<"-1"<<endl;
	else{for(i=0;i<Q;i++)cout<<dp[a[i]]<<endl;}
}

C++: #include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int n, m;
int s[400], l[400], p[400], w[400];
int dp[400];

int main(void){
    cin >> n;
    for(int i = 0; i < n; i++) cin >> s[i] >> l[i] >> p[i];
    cin >> m;
    for(int i = 0; i < m; i++) cin >> w[i];

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

    vector<int> res;
    bool flag = true;
    for(int i = 0; i < m; i++){
        if(dp[w[i]] == 0) flag = false;
        res.push_back(dp[w[i]]);
    }

    if(flag){
        for(auto x: res) cout << x << '\n';
    }else{
        cout << -1 << '\n';
    }

    return 0;
}