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>
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 1002
#define i197 1000000007
#define Vi vector<int>
#define Vl vector<long long>
#define pb(a) push_back(a)

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<P1,ll> P2;
const ll INF=100000000001;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct edge{int to,cost;};
struct pos{
	int x,y,cost;
};
int dp[394][394]={};
int s[394],l[394],p[394];
int ans[394]={};

int main() {

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

	return 0;
}

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

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

int n, m;
int s[400], l[400], p[400];
int w[400];

int dp[400][401];

int main() {
    cin >> n;
    rep(i, n) {
        cin >> s[i] >> l[i] >> p[i];
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= 400; ++j) {
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            for (int k=s[i]; k<=l[i]; ++k) {
                if (j >= k) {
                    dp[i+1][j]=max({dp[i][j], dp[i+1][j], dp[i+1][j-k]+p[i]});
                }
                //else dp[i+1][j] = max(dp[i][j], dp[i+1][j]);
            }
        }
    }
    cin >> m;
    bool ok = true;
    rep(i, m) {
        cin >> w[i];
        if (dp[n][w[i]] == 0) {
            ok = false;
        }
    }
    if (!ok) {
        cout << -1 << endl;
        return 0;
    }
    rep(i, m) {
        cout << dp[n][w[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))
using namespace std;
int n, w, dp[393+1][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 <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;
		state ccopy = combi;
		while(incopy.down < MAX){
			if(incopy.up > MAX) incopy.up = MAX;
			if(incopy.up+ccopy.up > MAX) ccopy.up = MAX-incopy.up;
			for(int j = incopy.down; j <= incopy.up; j++){
				if(table[j]<incopy.point)table[j] = incopy.point; 
			}
			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; 
			}
			/*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;
			ccopy.down += combi.down;
			ccopy.up += combi.up;
			ccopy.point += combi.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++: // AtCode.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <stdint.h>
#include <queue>
#include <bitset>

using namespace std;

#define MAX_NUM (393)

int s[MAX_NUM];
int l[MAX_NUM];
int p[MAX_NUM];

//dp[i][j]: i番目のフレーズまでを使た場合の、長さjフレーズの最高得点
int dp[MAX_NUM + 1][MAX_NUM + 1];

int main() {

	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s[i] >> l[i] >> p[i];
	}

	for (int id = 1; id < n + 1; id++) {
		for (int length = 0; length < MAX_NUM + 1; length++) {
			dp[id][length] = dp[id - 1][length];
			for (int use = s[id - 1]; use < min(length,l[id - 1])+1; use++) {
				dp[id][length] = max(dp[id][length], dp[id][length - use] + p[id - 1]);
			}
		}
	}

	int m;
	cin >> m;
	vector<int> ans;
	for (int i = 0; i < m; i++) {
		int tmp;
		cin >> tmp;
		ans.push_back(dp[n][tmp]);
		if (dp[n][tmp] == 0) {
			cout << "-1" << endl;
			return 0;
		}
	}

	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << endl;
	}


	return 0;
}