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++) {
			for (int k=S[i]; k<=L[i]; k++) {
				dp[i][j+k] = max(max(dp[i][j+k], 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 <vector>
#include <algorithm>
using namespace std;

int main(){
	vector<int> v(394, -1000000000);
	v[0] = 0;
	int n, m, s, l, p, w;
	cin >> n;
	for(int i = 0; i < n; ++i){
		cin >> s >> l >> p;
		for(int j = s; j < 394; ++j){
			for(int k = min(j, l); k >= s; --k){
				v[j] = max(v[j], v[j-k] + p);
			}
		}
	}

	cin >> m;
	vector<int> ans(m);
	for(int i = 0; i < m; ++i){
		cin >> w;
		ans[i] = v[w];
	}
	if(*min_element(ans.begin(), ans.end()) < 0){
		ans.assign(1, -1);
	}
	for(int i = 0; i < ans.size(); ++i){
		cout << ans[i] << '\n';
	}
}

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

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
int n;
int s[400],l[400],p[400];
int m;
int w[400];
int dp[400][400]= {0};//dp[i番目まで見たとき][メロディ長]
int main(){
    cin >> n;
    
    for(int i=1;i<=n;i++){
        cin >> s[i] >> l[i] >> p[i];
        for(int j=s[i];j<=l[i];j++){
            dp[i][j] = max(dp[i][j],p[i]);
        }
    }
    for(int i=0;i<396;i++){
        if(i>=l[1]){
            for(int j=s[1];j<=l[1];j++){
                if(dp[1][i-j]!=0){
                    dp[1][i] = max(dp[0][i],dp[1][i-j]+p[1]);
                }
            }
        }
    }
for(int i=1;i<=n;i++){
        for(int j=1;j<=396;j++){
            dp[i+1][j] = max(dp[i][j],dp[i+1][j]);
            if(j>=s[i]){
                for(int k=s[i];k<=l[i]&&k<=j;k++){
                    if(dp[i+1][j-k]!=0){
                        dp[i+1][j] = max(dp[i+1][j],dp[i+1][j-k]+p[i]);
                    }
                }
            }
        }
    }

    cin >> m;
    bool checker = false;
    for(int i=1;i<=m;i++){
        cin >> w[i];
        if(dp[n][w[i]]==0){
            checker = true;
        }
    }

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

}

C++: #include <algorithm>
#include <iostream>
#define N 394
using namespace std;

int main(){
  int n,s[N],l[N],p[N],dp[N][N],m,w,ans[N],flag=0;
  cin>>n;
  for(int i=1;i<=n;i++) cin>>s[i]>>l[i]>>p[i];
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++) dp[i][j]=-1;
  dp[0][0]=0;
  for(int i=1;i<=n;i++){
    for(int j=0;j<N;j++){
      dp[i][j]=max(dp[i][j],dp[i-1][j]);
      for(int k=s[i];k<=l[i];k++){
	if(j-k>=0&&dp[i][j-k]!=-1) dp[i][j]=max(dp[i][j],dp[i-1][j-k]+p[i]);
      }
    }
  }
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    if(dp[n][w]==-1){
      flag=1;
      break;
    }else ans[i]=dp[n][w];
  }
  if(!flag)
    for(int i=0;i<m;i++) cout<<ans[i]<<endl;
  else cout<<-1<<endl;
  return 0;
}

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);
					}
				}
			}
		}
	}

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