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;

typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

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

	int dp[400];
	fill(dp, dp+400, -1);
	dp[0] = 0;

	rep(i, 400){
		rep(j, n){
			for(int k = s[j]; k <= l[j]; k++){
				if(i-k >= 0) dp[i] = max(dp[i], dp[i-k]+p[j]);
			}
		}
	}
	vector<int> ret(m);
	rep(i, m){
		if(dp[w[i]] == -1){cout << -1 << endl; return 0;}
		ret[i] = dp[w[i]];
	}
	rep(i, m) cout << ret[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
struct P {int x,y,z;};
typedef pair<double,int> PP;

int main() {
  int n;
  cin >> n;
  P p[n];
  PP a[n];
  for(int i=0; i<n; i++) {
    int x,y,z;
    cin >> x >> y >> z;
    p[i]=(P){x,y,z};
    a[i]=PP((double)z/x,i);
  }
  sort(a,a+n,greater<PP>());
  vector<int> ans;
  int c[400];
  memset(c,0,sizeof(c));
  for(int i=0; i<400; i++) {
    int r=i;
    for(int k=0; k<n; k++) {
      while(r>=p[a[k].second].x) {
        r-=p[a[k].second].x;
        c[i]+=p[a[k].second].z;
      }
    }
    if(r) c[i]=-1;
  }
  int m;
  cin >> m;
  while(m--) {
    int M=-1,e;
    cin >> e;
    for(int i=0; i<n; i++) {
      for(int j=p[i].x; j<=p[i].y; j++) {
        int r=e-j,d=p[i].z;
        if(r<0) continue;
        else if(c[r]!=-1) M=max(M,d+c[r]);
      }
    }
    if(M==-1) {
      cout << -1 << endl;
      return 0;
    } else ans.push_back(M);
  }
  for(int i=0; i<ans.size(); i++) cout << ans[i] << endl;
  return 0;
}

C++: #include<iostream>
#include<vector>
#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=-1;
    }

};

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;

    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);
            }
        }
    }

    vector<int> ans;
    bool flag = false;
    
    rep(i,m) {
        int t = dp[w[i]];
        if(t < 0) flag=true;
        else ans.push_back(t);
    }
    if(flag) {
        cout<<"-1"<<endl;
        return 0;
    }

    rep(i,ans.size()) cout<<ans[i]<<endl;

    return 0;
}

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

using namespace std;

int main()
{
    
    int n,s,l,p,m,f,w[410];
    int dp[410][410]={};
    
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> s >> l >> p;
        for(int j=0;j<400;j++){
            dp[i][j] = dp[i-1][j];
            for(int k=s;k<=l;k++){
                if(j-k < 0) continue;
                dp[i][j] = max(dp[i][j],dp[i][j-k]+p);
            }
        }
    }
    
    cin >> m;
    f = 0;
    for(int i=0;i<m;i++){
        cin >> w[i];
        if(dp[n][w[i]] == 0) f=1;
    }
    
    if(f == 0){
        for(int i=0;i<m;i++) cout << dp[n][ w[i] ] << endl;
    } else {
        cout << -1 << endl;
    }
    
    return(0);
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
using ll = long long;
using namespace std;

int w[401];
vector<tuple<int, int, int>> v;
int dp[401]; //indexの番号 重さ 最大コストをもつ
int const MOD = 1e9 + 7;
int main(void)
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        int r, l, p;
        cin >> r >> l >> p;
        v.push_back(make_tuple(r, l, p));
    }

    int m;
    cin >> m;
    for (int i = 0; i < m; ++i)
        cin >> w[i];

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= 12; ++j)
        {
            for (int k = get<0>(v[i]); k <= get<1>(v[i]); ++k)
            {
                if (j - k >= 0)
                {
                    dp[j] = max(dp[j], dp[j - k] + get<2>(v[i]));
                }
                else
                {
                    dp[j] = dp[j];
                }
            }
        }
    }

    bool ok = true;
    vector<int> ans;
    for (int i = 0; i < m; ++i)
    {
        if (dp[w[i]] == 0)
            ok = false;
        ans.push_back(dp[w[i]]);
    }
    if (!ok)
        cout << -1 << endl;
    else
    {
        for (auto e : ans)
            cout << e << endl;
    }
    return 0;
}

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(i&&j-s[i-1]>=0&&k-(l[i-1]-s[i-1])>=0)
	  if(dp[i][j-s[i-1]][k-(l[i-1]-s[i-1])])
	    dp[i][j][k]=max(dp[i][j][k],dp[i][j-s[i-1]][k-(l[i-1]-s[i-1])]+p[i-1]);
	
	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<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

#define REP(i, n) for(int i=0;i<(int)n;i++)
#define rep(i, n) for(int i=0;i<(int)n;i++)
#define reep(i, s, t) for(int i=s;i<(int)t;i++)
#define irep(i, n) for(int i=(int)n-1;i>=0;i--)
#define ireep(i, n, s) for(int i=(int)n-1;i>=s;i--)
#define all(v) v.begin(), v.end()
#define vc vector
#define pb push_back

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

int n,m;
const int INF = 1<<25;
int dp[1005];
int main(){
	cin >> n;
	REP(i, 400) dp[i] = -INF;
	dp[0] = 0;
	REP(tttt, n){
		int s, t, w;
		cin >> s >> t >> w;
		irep(i, 400){
			for(int j=s;j<=t;j++) dp[i+j] = max(dp[i+j], dp[i] + w);
		}
	}
	vi ans;
	int f = 1;
	cin >> m;
	REP(i, m){
		int x;
		cin >> x;
		ans.pb(dp[x]);
		if(dp[x] < 0) f = 0;
	}
	if(!f) cout << -1 << endl;
	else{
		for(int v : ans) cout << v << endl;
	}
	return 0;
}