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

    rep(i,n) {
        for(int j=phra[i].s;j<=phra[i].l;j++) {
            for(int k=j;k<400;k++) {
                dp[k] = max(dp[k],dp[k-j]+phra[i].p);
            }
        }
    }

    multiset<int> ans;
    bool flag = false;
    
    rep(i,m) {
        int t = dp[w[i]];
        if(t < 0) flag=true;
        ans.insert(dp[w[i]]);
    }
    if(flag || ans.find(-1) != ans.end()) {
        cout<<"-1"<<endl;
        return 0;
    }

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

    return 0;
}

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;
int n, w, dp[10000][393+1];
vector<int>p, len;

int rec(int i, int j){
  if(dp[i][j] >= 0) return dp[i][j];
  int res;
  if(i == p.size() || j == 0) res = 0;
  else if(j < len[i]) res = rec(i + 1, j);
  else  res = max(rec(i + 1, j), rec(i + 1, j - len[i]) + p[i]);

  return dp[i][j] = res;
}

int main(){
  int n, m, s, l, point;
  memset(dp, -1, sizeof(dp));
  scanf("%d", &n);
  rep(i, n){
    scanf("%d%d%d", &s, &l, &point);
    for(int j = s; j <= l; j++){ len.push_back(j); p.push_back(point);}
  }
  scanf("%d", &m);
  vector<int>res;
  rep(i, m){
    scanf("%d", &w);
    res.push_back(rec(0, w));
  }
  bool f = true;
  rep(i, res.size()) if(res[i] == 0) f = false;
  if(f) rep(i, res.size()) printf("%d\n", res[i]);
  else puts("-1");
  return 0;
}

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

using namespace std;

#define range(i, a, b) for(int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define s first.first
#define l first.second 
#define p second

typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 394;

int main(){
	int n; cin >> n;
	vector< pair<pii, int> > f(n);

	rep(i, n) cin >> f[i].s >> f[i].l >> f[i].p;

	vi dp(N);
	fill(all(dp), -1);
	dp[0] = 0;

	rep(i, n)
		range(j, f[i].s, f[i].l + 1)
			rep(k, N)
				if(dp[k] != -1 && k + j < N)
					dp[k + j] = max(dp[k + j], dp[k] + f[i].p);

	int m; cin >> m;
	vi res(m);

	rep(i, m){
		int w; cin >> w;

		res[i] = dp[w];
	}

	if(find(all(res), -1) != res.end()) cout << -1 << endl;
	else rep(i, m) cout << res[i] << endl;


	return 0;
}

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

using namespace std;

struct MEL{int s,l,p;};
const int MAX = 400;
int dp[MAX];
vector<MEL> V;

void solve(){

  for(int i = 0; i < V.size(); i++)
    for(int j = V[i].s; j <= V[i].l; j++)
      for(int k = 0; k < MAX && k+j < MAX; k++)
	if(dp[k] > 0 || k == 0) dp[k+j] = max(dp[k+j],dp[k]+V[i].p); 
      

  vector<int> ans;
  int m;
  cin >> m;
  while(m--){
    int in;
    cin >> in;
    if(dp[in] == -1){
      cout << "-1" << endl;
      return;
    }
    ans.push_back(dp[in]);
  }
  for(int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
}

int main(){

  int n;
  cin >> n;
  while(n--){
    MEL mel;
    cin >> mel.s >> mel.l >> mel.p;
    V.push_back(mel);
  }
  solve();
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
using ll = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;};

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

  Int n;
  cin>>n;
  vector<Int> ss(n),ls(n),ps(n);
  for(Int i=0;i<n;i++) cin>>ss[i]>>ls[i]>>ps[i];

  const Int MAX = 500;
  const Int INF = 1e15;
  vector<Int> dp(MAX,-INF);
  dp[0]=0;
  for(Int i=0;i<n;i++)
    for(Int j=0;j<MAX;j++)
      for(Int k=ss[i];k<=ls[i];k++)
        if(j+k<MAX) chmax(dp[j+k],dp[j]+ps[i]);

  Int m;
  cin>>m;
  vector<Int> ws(m);
  for(Int i=0;i<m;i++) cin>>ws[i];

  auto NG=[](){cout<<-1<<endl;exit(0);};
  for(Int i=0;i<m;i++)
    if(dp[ws[i]]<0) NG();

  for(Int i=0;i<m;i++) cout<<dp[ws[i]]<<endl;
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

int main() {
    int n, m, w;
    cin >> n;
    vector<int> s(n), l(n), p(n);
    for (int i = 0; i < n; i++) cin >> s.at(i) >> l.at(i) >> p.at(i);
    vector<vector<int>> dp(n + 1, vector<int>(394));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 394; j++) {
            int d = dp.at(i).at(j);
            for (int k = s.at(i); k <= l.at(i); k++) {
                if (j < k) break;
                d = max(d, dp.at(i + 1).at(j - k) + p.at(i));
            }
            dp.at(i + 1).at(j) = d;
        }
    }
    cin >> m;
    vector<int> ans(m);
    for (int i = 0; i < m; i++) {
        cin >> w;
        ans.at(i) = dp.at(n).at(w);
        if (ans.at(i) == 0) ans.at(0) = 0;
    }
    for (int i = 0; i < m; i++) {
        if (ans.at(i) == 0) {
            cout << -1 << endl;
            break;
        }
        cout << ans.at(i) << endl;
    }
}