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<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef vector<string>vs;
typedef vector<int>vi;
typedef vector<vi>vvi;
typedef vector<double>vd;
typedef pair<int,int>pii;
typedef long long ll;
typedef pair<ll,ll>pll;
typedef vector<ll>vl;
#define rrep(i,x,n) for(int i=(x);i<(n);++i)
#define rep(i,x) rrep(i,0,(x))
#define fi first
#define se second
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define pb push_back
#define maxs(a,b) (a)=max(a,b)
#define mins(a,b) (a)=min(a,b)

int main(){
    int N;
    int l[400],u[400],p[400];
    scanf("%d",&N);
    rep(i,N)scanf("%d%d%d",&l[i],&u[i],&p[i]);

    int dp[400];fill_n(dp,400,-1);
    dp[0]=0;

    for(int i=0;i<N;i++){
        for(int j=0;j<=393;j++){
            for(int k=l[i];k<=u[i];k++){
                if(j-k<0||dp[j-k]==-1)continue;
                dp[j]=max(dp[j],dp[j-k]+p[i]);
            }
        }
    }
    int ans[400];
    int M;
    scanf("%d",&M);
    for(int i=0;i<M;i++){
        int w;
        scanf("%d",&w);
        if(dp[w]==-1){
            puts("-1");
            return 0;
        }
        ans[i]=dp[w];
    }

    for(int i=0;i<M;i++)printf("%d\n",ans[i]);
}

C++: 
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
template<class T> inline bool amax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool amin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> ostream& operator << (ostream &os, const vector<T> &v) { os << "["; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) { os << (it != v.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T> &s) { os << "["; for (typename set<T>::const_iterator it = s.begin(); it != s.end(); it++) { os << (it != s.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class Key, class Val> ostream& operator << (ostream &os, const map<Key, Val> &m) { os << "{"; for (typename map<Key, Val>::const_iterator it = m.begin(); it != m.end(); it++) { os << (it != m.begin() ? ", " : "") << it->first << ":" << it->second; } os << "}"; return os; }
template<class T, class S> ostream& operator << (ostream &os, const pair<T, S> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <class T> istream& operator >> (istream &is, vector<T> &v) { for (size_t i = 0; i < v.size(); i++) is >> v[i]; return is; }
template <class Target, class Source> inline Target lexical_cast (const Source &s) { Target t; stringstream ss; ss << s; ss >> t; return t; }

//> v < ^ (clock wise)
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
const int INFI = 1<<28;
const long long int INFL = 1LL<<60;
const double INFD = 1e+300;
const float INFF = 1e+100;
const double EPS = 1e-8;
const long long int MOD = 1000000007;

int solve ();
int main () {
    cout.setf(ios::fixed); cout.precision(10);
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}

int solve () {
    int n;
    cin >> n;
    int s, l, p;
    int score[160000];
    for (int i = 0; i < 160000; i++) score[i] = -INFI;
    score[0] = 0;
    for (int i = 0; i < n; i++) {
        cin >> s >> l >> p;
        for (int i = 160000-1; i >= 0; i--) {
            for (int j = s; j <= l; j++) {
                if (i+j < 160000) score[i+j] = max(score[i+j], score[i]+p);
            }
        }
    }

    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        int w;
        cin >> w;
        cout << (score[w]<0?-1:score[w]) << endl;
    }

    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 = 0; j < 394; j++){
      if(dp[j] == -1) continue;
      for(int k = s[i]; k <= l[i] && i + k < 394; k++){
        dp[j + k] = max( dp[j + k], dp[j] + p[i]);
      }
    }
  }
  cin >> m;
  while(m--){
    cin >> w;
    cout << dp[w] << endl;
  }
  return 0;
}

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

using namespace std;

int MAX = 393;

int main() {
	int n;
	cin >> n;

	vector<int> S(n), L(n), P(n), SUB(n);
	for (int i=0; i<n; i++) {
		cin >> S[i] >> L[i] >> P[i];
		SUB[i] = L[i]-S[i];
	}

	int m;
	cin >> m;

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

	int **dp = new int*[MAX+10];
	for (int i=0; i<MAX+10; i++) {
		dp[i] = new int[2*MAX];
		for (int j=0; j<2*MAX; j++)
			dp[i][j] = 0;
	}

	dp[0][0] = 0;
	for (int i=0; i*S[0]<=MAX; i++)
		for (int j=i*S[0]; j<=i*S[0]+SUB[0]; j++)
			dp[0][j] = max(dp[0][j], i*P[0]);
	
	for (int i=1; i<n; i++) {
		for (int j=0; j<=MAX; j++) {
			dp[i][j] = max(dp[i][j], dp[i-1][j]);
			for (int l=0; l*S[i]<=MAX; l++) {
				for (int k=l*S[i]; k<l*S[i]+SUB[i]; k++) {
					dp[i][j+k] = max(max(dp[i][j+k], dp[i-1][j+k]), dp[i-1][j]+l*P[i]);
				}
			}
		}
	}

	vector<int> point(m);
	bool flag = false;
	for (int i=0; i<m; i++) {
		point[i] = dp[n][W[i]];
		for (int j=0; j<n; j++)
			point[i] = max(point[i], dp[j][W[i]]);
		if (!point[i]) {
			flag = true;
			break;
		}
	}
	
	if (flag) cout << -1 << endl;
	else {
		for (int i=0; i<m; i++)
			cout << point[i] << endl;
	}

	for (int i=0; i<MAX+10; i++)
		delete [] dp[i];
	delete [] dp;

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define INF 1e9
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()


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