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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define MAX_A 394
ll dp[MAX_A];

int main(){
	ll N,M;
	scanf("%lld",&N);

	vector<ll> s(N),l(N),p(N);
	for (int i = 0; i < N; i++) {
		scanf("%lld",&s[i]);
		scanf("%lld",&l[i]);
		scanf("%lld",&p[i]);
	}
	scanf("%lld",&M);
	vector<ll> w(M);
	for (int i = 0; i < M; i++) {
		scanf("%lld",&w[i]);
	}

	for (ll i = 0; i < N; i++) {
		for (ll j = 1; j < MAX_A; j++) {
			for (ll k = max(j-l[i],0LL); k < max(j-s[i]+1,0LL); k++) {
				dp[j] = max(dp[j], dp[k]+p[i]);
			}
		}
	}
	bool flg = false;
	for (int i = 0; i < M; i++) {
		if (dp[w[i]] == 0) flg = true;
	}
	if (flg) {
		printf("-1\n");
	} else {
		for (int i = 0; i < M; i++) {
			printf("%lld\n", dp[w[i]]);
		}
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
  int n, m, s[393], l[393], p[393], w;
  vector< int > dp( 394, -1); //dp[i]:i長のメロディを作るための最大値
  dp[0] = 0;
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> s[i] >> l[i] >> p[i];
  }
  for(int i = 0; i < n; i++){
    for(int j = 393; j >= 0; j--){
      if(dp[j] == -1) continue;
      for(int k = s[i]; k <= l[i] && j + k < 394; k++){
        dp[j + k] = max( dp[j + k], dp[j] + p[i]);
      }
    }
  }
  cin >> m;
  vector< int > ret(m);
  bool flag = true;
  for(int i = 0; i < m; i++){
    cin >> w;
    ret[i] = dp[w];
    flag &= ret[i] != -1;
  }
  if(flag){
    for(int i = 0; i < m; i++){
      cout << ret[i] << endl;
    }
  }else{
    cout << -1 << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstring>
#include <stack>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
vector<pair<double,int> > member;
const double eps=1e-9;
typedef pair<int,int> p;
int dp[400][400];
int rest[400][400];
int main()
{
  int n;
  cin >> n;
  vector<int> s(n+5);
  vector<int> l(n+5);
  vector<int> p(n+5);
  for(int i=0;i<n;i++)
    {
      cin >> s[i] >> l[i] >> p[i];
    }
  int m;
  cin >> m;
  vector<int> score(m+5);
  bool clear=true;
  for(int i=0;i<m;i++)
    {
      int length;
      cin >> length;
      memset(dp,0,sizeof(dp));
      memset(rest,0,sizeof(rest));
      for(int j=0;j<n;j++)
	{
	  for(int k=0;k<=length;k++)
	    {
	      int mu=s[j];
	      if(k<mu)
		{
		  dp[j+1][k]=dp[j][k];
		  rest[j+1][k]=rest[j][k];
		}
	      else
		{
		  dp[j+1][k]=max(dp[j][k],dp[j+1][k-mu]+p[j]);
		  if(dp[j+1][k]>dp[j][k]) rest[j+1][k]=rest[j+1][k]+(l[j]-s[j]);
		  else rest[j+1][k]=rest[j][k];
		}
	      
	    }
	}
      int tmpmax=0;
      for(int j=0;j<=length;j++)
	{
	  if(rest[n][j]>=length-j) tmpmax=max(tmpmax,dp[n][j]);
	}
      if(tmpmax==0) 
	{
	  clear=false;
	  break;
	}
      else
	{
	  score[i]=tmpmax;
    	}
    }
  if(!clear) cout << -1 << endl;
  else
    {
      for(int i=0;i<m;i++)
	{
	  cout << score[i] << endl;
	}
    }
  
  return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = (a); i < (n); i++)
#define MP make_pair
#define all(s) (s).begin(), (s).end()
#define rev(s) (s).rbegin(), (s).rend()
#define X first
#define Y second

using namespace std;

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

int main(){
	//freopen("test.txt", "r", stdin);
	int n;
	cin >> n;

	vector<pair<int, P> > v(n);
	rep(i, n){
		cin >> v[i].Y.X >> v[i].Y.Y >> v[i].X;
	}
	sort(all(v));

	vi dp(400, -1);
	rep(w, 400) rep(i, n){
		if(v[i].Y.X <= w && w <= v[i].Y.Y){
			dp[w] = v[i].X;
			break;
		}
	}

	rep(w, 400){
		for(int i = w/2; i < w; i++){
			if(dp[i] == -1 || dp[w-i] == -1) continue;
			dp[w] = max(dp[w], dp[w-i]+dp[i]);
		}
	}

	int m;
	cin >> m;
	vi res;
	rep(i, m){
		int w;
		cin >> w;
		if(dp[w] != -1) res.push_back(dp[w]);
	}

	if(res.size() != m){
		cout << -1 << endl;
	}else{
		rep(i, m){
			cout << res[i] << endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll a7=1000000007;
ll inf=1000000000000000;
typedef pair<ll,ll> l_l;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back


ll min(ll a,ll b){
    if(a<b)return a;
    return b;
}
ll max(ll a,ll b){
    if(a>b)return a;
    return b;
}

int main(){
    ll n;cin>>n;
    ll s[n+1],l[n+1],p[n+1];
    rep(i,1,n){
        cin>>s[i]>>l[i]>>p[i];
    }
    ll m;cin>>m;
    ll w[m];
    rep(i,0,m-1){
        cin>>w[i];
    }
    
    ll dp[n+1][410];
    rep(i,0,n){
        rep(j,0,400){
            dp[i][j]=-1;
        }
        dp[i][0]=0;
    }
    ll maxi;
    rep(i,1,n){
        rep(j,1,400){
            if(j-s[i]<0){
                dp[i][j]=dp[i-1][j];
            }else{
                maxi=-1;
                rep(k,max(0,j-l[i]),j-s[i]){
                    maxi=max(maxi,dp[i][k]);
                }
                if(maxi==-1){
                    dp[i][j]=dp[i-1][j];
                }else{
                    dp[i][j]=max(maxi+p[i],dp[i-1][j]);
                }
            }
        }
    }
    /*
    rep(i,0,n){
        rep(j,0,15){
            cout<<dp[i][j]<<' ';
        }
        cout<<endl;
    }
     */
    bool b=false;
    ll ans[m];
    rep(i,0,m-1){
        if(dp[n][w[i]]==-1){
            b=true;
            break;
        }else{
            ans[i]=dp[n][w[i]];
        }
    }
    if(b){
        cout<<-1<<endl;
    }else{
        rep(i,0,m-1){
            cout<<ans[i]<<endl;
        }
    }
    
    return 0;
}

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

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

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

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