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];

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

    multiset<int> ans;
    
    rep(i,m) ans.insert(dp[w[i]]);    
    if(ans.find(0) != 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>
using namespace std;

const int INF = 1<<29;
int dp[400][400]; //dp[i]:=長さiのメロディで得られる最大得点.

int main(){
    int n, m;
    int point[393]={0}, w[393]={0};
    bool can = true;

    cin >> n;
    for(int i=0; i<n; i++){
        int l, r, p;
        cin >> l >> r >> p;
        for(int j=l; j<=r; j++) point[j] = max(point[j], p);
    }

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

    for(int i=0; i<m; i++){ //main loop

        for(int j=1; j<=w[i]; j++){
            for(int k = j; k <= w[i]; k++){
				if(k-j == 0 || dp[i][k-j]  != 0){
					dp[i][k] = max(dp[i][k],dp[i][k-j]+point[j]);
				}
			}
        }
        if(dp[i][w[i]] == 0){
            can = false;
            break;
        }
    }
    if(can){
        for(int i=0; i<m; i++) cout << dp[i][w[i]] << endl;
    }else{
        cout << -1 << endl;
    }
    return 0;
}

C++: #include <queue>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <set>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int main(){
  const int n = getInt();
  vector<int> l(n);
  vector<int> h(n);
  vector<int> p(n);

  REP(i,n){
    l[i] = getInt();
    h[i] = getInt();
    p[i] = getInt();
  }

  vector<int> dp(400, -1);
  dp[0] = 0;

  REP(i,n) REP(j,dp.size()) if(dp[j] != -1){
    for(int k = l[i]; k <= h[i] && j + k < (int)dp.size(); k++){
      dp[j + k] = max(dp[j + k], dp[j] + p[i]);
    }
  }

  const int m = getInt();
  vector<int> ans(m);
  REP(i,m){
    ans[i] = dp[getInt()];
    if(ans[i] == -1){
      puts("-1");
      return 0;
    }
  }
  REP(i,m) printf("%d\n", ans[i]);

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=5e18;

int main() {
  const ll num=393;
  ll n;
  cin >> n;
  vec s(n),l(n),p(n);
  for(ll i=0;i<n;i++) {
    cin >> s[i] >> l[i] >> p[i]; 
  }
  vec dp(num+1);
  for(ll i=0;i<n;i++) {
    for(ll j=0;j<=num;j++) {
      for(ll k=s[i];k<=l[i];k++) {
        if(j-k>=0) {
          dp[j]=max(dp[j],dp[j-k]+p[i]);
        }
      }
    }
  }
  ll m;
  cin >> m;
  vec w(m);
  for(ll i=0;i<m;i++) {
    cin >> w[i];
    if(dp[w[i]]==0) {
      cout << -1 << endl;
      return 0;
    }
  }
  for(ll i=0;i<m;i++) {
    cout << dp[w[i]] << endl;
  }
}

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

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

typedef long long ll;

int dp[1024];

int s[512], l[512], p[512];
int w[512];
int ans[512];

int main(){
    int n,m;
    cin >> n;
    rep(i,n) cin >> s[i] >> l[i] >> p[i];
    rep(i,394) rep(j,n) loop(len,s[j],l[j]+1) dp[i+len] = max(dp[i] + p[j], dp[i+len]);
    cin >> m;
    rep(i,m){
        int t;
        cin >> t;
        if(dp[t]==0){
            cout << -1 << endl;
            exit(0);
        } else {
            ans[i] = dp[t];
        }
    }
    rep(i,m) cout << ans[i] << endl;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define MAX 393
using namespace std;

struct state{
	state(int a, int b, int c):down(a), up(b), point(c){}
	int down;
	int up;
	int point;
};

int main(){
	int n = 0;
	cin >> n;
	vector<int> table(MAX+1, -1);
	state combi(0, 0, 0);
	for(int i = 0; i < n; i++){
		state input(0, 0, 0);
		cin >> input.down >> input.up >> input.point;
		state incopy = input;
		while(incopy.down < MAX){
			if(incopy.up > MAX) incopy.up = MAX;
			for(int j = incopy.down; j <= incopy.up; j++){
				if(table[j]<incopy.point)table[j] = incopy.point; 
			}
			state ccopy = combi;
			while(ccopy.down&&incopy.down+ccopy.down < MAX){
				if(incopy.up+ccopy.up > MAX) ccopy.up = MAX-incopy.up;
				for(int j = incopy.down+ccopy.down; j <= incopy.up+ccopy.up; j++){
					if(table[j]<incopy.point+ccopy.point)table[j] = incopy.point+ccopy.point; 
				}
				ccopy.down += combi.down;
				ccopy.up += combi.up;
				ccopy.point += combi.point;
			}
			incopy.down += input.down;
			incopy.up += input.up;
			incopy.point += input.point;
		}
		combi.down += input.down;
		combi.up += input.up;
		combi.point += input.point;
	}

	queue<int> q;
	int m = 0;
	cin >> m;
	for(int i = 0; i < m; i++){
		int tmp = 0;
		cin >> tmp;
		if(table[tmp]!=-1) q.push(table[tmp]);
		else{
			cout << "-1" << endl;
			return 0;
		}
	}
	while(!q.empty()){
		cout << q.front() << endl;
		q.pop();
	}

	return 0;
}

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

using namespace std;

int main(){
  int n;
  cin>> n;
  int d[400],u[400],s[400];
  for(int i=0;i<n;i++) cin>>d[i]>>u[i]>>s[i];
  
  int dp[400]={};
  memset(dp,-1,sizeof(dp));
  dp[0]=0;

  for(int i=0;i<n;i++)
    for(int k=u[i];k>=d[i];k--)
      for(int j=399;j>=0;j--)
	if(j+k<400 && dp[j]!=-1) dp[j+k] = max(dp[j+k],dp[j]+s[i]);
      
  int q,flg=0,m[400];
  cin>> q;
  for(int i=0;i<q;i++){
    cin >> m[i];
    if(dp[m[i]] == -1) flg= 1;
  }
  if(flg) cout << -1<<endl;
  else 
    for(int i=0;i<q;i++) cout << dp[m[i]]<<endl;
  return 0;
}