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<map>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

int point[393];
int dp[394];
pair<int,int>len[393];
int n;

void check(int mmin,int max,int i,int pp)
{
  // cout<<mmin<<" "<<max<<" "<<i<<" "<<pp<<endl;
  if(mmin>393||i>=n)
    return;
  int vmax=min(max,393);
  bool cnt=false;
  for(int j=mmin;j<=vmax;j++)
    {
      if(dp[j]<pp)
	{
	  dp[j]=pp;
	  cnt=true;
	}
    }
//  if(!cnt)
//    return;
  check(mmin,max,i+1,pp);
  check(mmin+len[i].first,max+len[i].second,i,pp+point[i]);
}

int main()
{
 
  cin>>n;
  for(int i=0;i<n;i++)
    {
      int s,l,p;
      cin>>s>>l>>p;
      len[i]=make_pair(s,l);
      point[i]=p;
    }
  int m;
  cin>>m;
  memset(dp,-1,sizeof(dp));
  check(0,0,0,0);
  vector<int> ans;
  bool flag=false;
  for(int i=0;i<m;i++)
    {
      int w;
      cin>>w;
      ans.push_back(dp[w]);
      if(dp[w]==-1)
	flag=true;

    }
  if(flag)
    cout<<"-1"<<endl;
  else
  for(int i=0;i<m;i++)
      cout<<ans[i]<<endl;
}

C++: #include<iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int dp[394];

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

C++: #include<bits/stdc++.h>
using namespace std;
int dp[400][400][400]={};
int main(){
  int n;
  cin>>n;
  int s[400],l[400],p[400];
  
  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(j-s[i]>=0&&k-(l[i]-s[i])>=0)
	  if(dp[i][j-s[i]][k-(l[i]-s[i])])
	    dp[i][j][k]=max(dp[i][j][k],dp[i][j-s[i]][k-(l[i]-s[i])]+p[i]);
	
	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]);
	
	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 <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))
#define MIKU 39
using namespace std;
int n, w, dp[MIKU*10+4][MIKU*10+4];

int p[MIKU*10+4], len[MIKU*10+4];
int rec(int i, int j){
  if(dp[i][j] >= 0) return dp[i][j];
  int res;
  if(i == MIKU*10+4 || j == 0) res = 0;
  else if(j < i) res = rec(i + 1, j);
  else  res = max(rec(i + 1, j), rec(i + 1, j - 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++) p[j] = max(p[j], 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;
inline void chmax (int& a, int b) {
  if (a < b) a = b;
}
void err() {
  cout << -1 << '\n';
  exit(0);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> dp(394, -1);
  dp[0] = 0;
  while (n--) {
    int s, l, p;
    cin >> s >> l >> p;
    for (int i = s; i <= 393; i++) {
      int &crr = dp[i];
      for (int j = max(0, i - l); j <= i - s; j++) {
        int &prv = dp[j];
        if (prv == -1) continue;
        chmax(crr, prv + p);
      }
    }
  }
  int q;
  cin >> q;
  vector<int> que;
  while (q--) {
    int w;
    cin >> w;
    int ret = dp[w];
    if (ret == -1) err();
    que.push_back(ret);
  }
  for (int ret : que) {
    cout << ret << '\n';
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,x) for(int i = 0; i < x; i++)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
const ll N = 1e9+7;
const int n_max = 1e5+10;

int main(){
    int n; cin >> n;
    vector<int> l(n),r(n),p(n);
    int point[400] = {},dp[400] = {};
    rep(i,n) {
        cin >> l[i] >> r[i] >> p[i];
        for(int j = l[i]; j <= r[i]; j++){
            point[j] = max(point[j], p[i]);
        }
    }
    // vector<int> dif(n);
    // rep(i,n) dif[i] = l[i] - s[i] + 1;
    int m; cin >> m;
    vector<int> w(m);
    rep(i,m) {
        cin >> w[i];
    }
    dp[0] = 0;
    rep(i,400){
        for(int j = i; j < 400; j++){
            dp[j] = max(dp[j], dp[j-i] + point[i]);
        }
    }

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

    // bool dp_s[400][400] = {}, dp_l[400][400] = {};
    // dp_s[0][0] = 1;dp_l[0][0] = 1;
    // rep(i,n+1){
    //     if(!i)continue;
    //     rep(j,400){
    //         for(int k = 0;k * s[i-1] <= 400;k++) {
    //             dp_s[i][j] |= dp_s[i-1][j- k * s[i-1]];
    //         }
    //         for(int k = 0;k * l[i-1] <= 400;k++) {
    //             dp_l[i][j] |= dp_l[i-1][j- k * l[i-1]];
    //         }
    //     }
    // }
    // int dp_total[400][400] = {};
    // rep(i,n+1){
    //     // bool ok = false;
    //     if(!i)continue;
    //     rep(j,400){
    //         dp_total[i][0] = 1;
    //         if(j) dp_total[i][j] = dp_total[i][j-1] + dp_s[i][j] - dp_l[i][j-1];
    //     }
    // }

    // rep(i,m){
    //     if(dp[n][w[i]]) cout << dp[n][w[i]] << endl;
    //     else cout << -1 << endl;
    // }


    return 0;

}