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 <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[30000][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 <bits/stdc++.h>
using namespace std;
typedef vector<int> Vi;
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; }

int dp[394][394];

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

    int n,m,dum; cin>>n; int w[n],v[n];
    for (int i = 0; i < n; i++)cin>>w[i]>>dum>>v[i];
    for (int i = 0; i < n; i++){
        for (int j = 0; j <= 393; j++){
            if(j<w[i])chmax(dp[i+1][j],dp[i][j]);
            else chmax(dp[i+1][j],max(dp[i][j],max(dp[i][j-w[i]]+v[i],dp[i+1][j-w[i]]+v[i])));
        }
    }
    cin>>m; Vi ans;
    for (int i = 0; i < m; i++){
        int q; cin>>q;
        if(!dp[n][q]){cout<<-1<<'\n'; return 0;}
        else ans.push_back(dp[n][q]);
    }
    for (int i = 0; i < m; i++)cout<<ans[i]<<'\n';
}

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