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 <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);
	int M = -1;
	for (int i=0; i<m; i++) {
		cin >> W[i];
		M = max(M, W[i]);
	}

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

	dp[0][0] = 0;
	//for (int i=1; i*S[0]<=M; i++)
	for (int j=S[0]; j<=L[0]; j++)
		dp[0][j] = max(dp[0][j], P[0]);
	
	for (int i=1; i<n; i++) {
		for (int j=0; j<=M; j++) {
			if (j < S[i]) {
				dp[i][j] = dp[i-1][j];
			} else {
				for (int k=S[i]; k<=L[i]; k++) {
					if (j-k >= 0) dp[i][j-k] = max(dp[i-1][j+k], dp[i][j]+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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int dp[400];
int table[400];
int main(){
	int n;
	cin >> n;
	for(int i = 0 ; i < 400 ; i++) table[i] = dp[i] = -1e9;
	for(int i = 0 ; i < n ; i++){
		int l,r,p;
		cin >> l >> r >> p;
		for(int j = l ; j <= r ; j++)
			table[j] = max(table[j],p);
	}
	dp[0] = 0;
	for(int i = 0 ; i < 400 ; i++){
		for(int j = i ; j < 400 ; j++){
			dp[j] = max(dp[j],dp[j-i]+table[i]); 
		}
	}
	vector<int> res;
	int m;
	cin >> m;
	for(int i = 0 ; i < m ; i++){
		int w;
		cin >> w;
		if( dp[w] < 0 ) {
			cout << -1 << endl;
			return 0;
		}
		res.push_back(dp[w]);
	}
	for(int i = 0 ; i < m ; i++)
		cout << res[i] << endl;
	
}

C++: #include <iostream>
using namespace std;

int main(void){
    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];
    int m; cin >> m;
    int w[400];
    for(int i = 0; i < m; i++) cin >> w[i];

    int dp[400] = {0}; // dp[i]: 長さiのうち最大の得点
    for(int i = 0; i < n; i++){ // i番目までのフレーズを使う
        for(int j = s[i]; j <= 393; j++){ // dp[j]を更新する
            for(int k = s[i]; k <= l[i]; k++){
                if(j >= k) dp[j] = max(dp[j], dp[j-k]+p[i]);
            }
        }
    }

    for(int i = 0; i < m; i++){
        if(dp[w[i]] == 0){
            cout << "-1\n";
            return 0;
        }
    }

    for(int i = 0; i < m; i++) cout << dp[w[i]] << '\n';
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(int argc, char *argv[]){
  int n, m;
  cin >> n;
  vector<int> s(n), l(n), p(n);
  vector<vector<int> > dp(n + 1, vector<int>(394, -1));
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> l[i] >> p[i];
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 394; j++) {
      for (int k = s[i]; k <= l[i]; k++) {
        if(j < k){
          dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);        
        }else{
          if(dp[i + 1][j - k] == -1 &&
             dp[i][j] == -1)continue;
          dp[i + 1][j] = max(dp[i + 1][j],
                             max(dp[i][j],
                                 dp[i + 1][j - k] + p[i]));
        }
      }
    }
  }
  cin >> m;
  int query;
  vector<int> ans;
  for (int i = 0; i < m; i++) {
    cin >> query;
    ans.push_back(dp[n][query]);
    if(ans.back() == -1){
      std::cout << -1 << std::endl;
      return 0;
    }
  }
  for (int i: ans) {
    std::cout << i << std::endl;
  }
  return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main(){
	int n;
	cin>>n;
	vi s(n),l(n),p(n);
	REP(i,n){
		cin>>s[i]>>l[i]>>p[i];
	}
	int m;
	cin>>m;
	vi w(m);
	REP(i,m){
		cin>>w[i];
	}
	vvi dp(n+1,vi(400+1));
	REP(i,n){
		REP(j,400+1){
			FOR(k,s[i],l[i]+1){
				if(j-k>=0){
					dp[i+1][j]=max(dp[i+1][j],dp[i][j-k]+p[i]);
				}
				dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
			}
		}
	}
	vi ans(m);
	bool flag=false;
	REP(melody,m){
		ans[melody]=dp[n][w[melody]];
		if(ans[melody]==0){
			flag=true;
		}
	}
	if(flag){
		cout<<-1<<endl;
	}else{
		REP(i,m){
			cout<<ans[i]<<endl;
		}
	}

}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>

using namespace std;

int main() {
	int n;

	cin >> n;

	vector< pair< int, pair< int, int > > > mel(n);

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

		mel[i].first = p;
		mel[i].second.first = s;
		mel[i].second.second = l;
	}

	int m;

	cin >> m;

	vector< vector< int > > dp(m, vector< int >(394, 0));
	vector< int > w(m);

	for(int i = 0; i < m; i++) {
		cin >> w[i];
	}


	for(int i = 0; i < m; i++) {
		for(int j = 0; j < n; j++) {
			dp[i][mel[j].second.first] += mel[j].first;
			dp[i][mel[j].second.second] += mel[j].first;
		}
	}

	for(int i = 0; i < m; i++) {
		for(int j = 0; j <= 393; j++) {
			if(dp[i][j] != 0) {
				for(int k = 0; k < n; k++) {
					if(j + mel[k].second.first <= 393) {
						dp[i][j + mel[k].second.first] = max(dp[i][j + mel[k].second.first] ,dp[i][j] + mel[k].first);
					}
					if(j + mel[k].second.second <= 393) {
						dp[i][j + mel[k].second.second] = max(dp[i][j + mel[k].second.second] ,dp[i][j] + mel[k].first);
					}
				}
			}
		}
	}

	bool flag = true;

	for(int i = 0; i < m; i++) {
		if(dp[i][w[i]] == 0) {
			flag = false;
			break;
		}
	}

	for(int i = 0; i < m; i++) {
		if(flag) {
			cout << dp[i][w[i]] << endl;
		} else {
			cout << -1 << endl;
			break;
		}
	}
}