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 <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(int rep=1;l[i]*rep<400;rep++){
				FOR(k,s[i]*rep,l[i]*rep+1){
					if(j-k>=0){
						dp[i+1][j]=max(dp[i+1][j],dp[i][j-k]+p[i]*rep);
					}
				}
			}
			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 <cstring>
#include <vector>
using namespace std;

int dp[400];
int n,s,l,p,w,ans[400];

int main(){

  for(int i = 0; i < 400;i++){
    dp[i] = -1;
  }
  dp[0] = 0;

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

  for(int i = 1; i < 400;i++){
    int max = -1;
    for(int j = 0; j <= (i)/2; j++){
      if(dp[j] != -1 && dp[i-j] != -1){
	if(max < dp[j] + dp[i-j]) max =  dp[j] + dp[i-j];
      } 
       }
    dp[i] = max;
  }

  for(int i = 0; i < 393 ;i++){
    //     cout << dp[i] << " ";
  }
  //   cout << endl;



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

  if(f != 1){
    for(int i = 0; i < ans.size() ;i++){
      cout << ans[i] << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<set>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class Phrase {
    public:
    int s,l,p;
    Phrase(int s,int l,int p) {

        this->s = s;
        this->l = l;
        this->p = p;
    }
    Phrase() {
        s=l=p=0;
    }

};

int n;
Phrase phra[400];
int m;
int w[400];
int dp[400];

int main() {

    cin>>n;
    rep(i,n) {
        int s,l,p;
        cin>>s>>l>>p;
        phra[i] = Phrase(s,l,p);
    }
    cin>>m;
    rep(i,m) cin>>w[i];
    rep(i,400) dp[i]=-1000000;
    dp[0]=0;

    rep(i,n) {
        for(int j=phra[i].s;j<=phra[i].l;j++) {
            for(int k=0;k<400;k++) {
                dp[k+j] = max(dp[k+j],dp[k]+phra[i].p);
            }
        }
    }

    multiset<int> ans;
    bool flag = false;
    
    rep(i,m) {
        int t = dp[w[i]];
        if(t < 0) flag=true;
        ans.insert(dp[w[i]]);
    }
    if(flag || ans.find(-1) != ans.end()) {
        cout<<"-1"<<endl;
        return 0;
    }

    multiset<int>::iterator it = ans.begin();
    while(it != ans.end()) {
        cout<<(*it)<<endl;
        it++;
    }

    return 0;
}

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

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

void dp(int num, int melodyLength, int countNum){
	if(melodyLength == w){
		score[countNum] = max(score[countNum], num);
	}

	for(int i=0; i < n; i++){
		for(int j=s[i]; j <= l[i]; j++){
			if(melodyLength + j <= w){
				dp(num + p[i], melodyLength + j, countNum);
			}
		}
	}
}

int main(){
	cin >> n;
	int count;
	for(int i=0; i < 400; i++) score[i] = -1;

	for(int i=0; i < n; i++){
		cin >> s[i] >> l[i] >> p[i];
	}
	cin >> m;
	for(count = 0; count < m; count++){
		cin >> w;
		dp(0, 0, count);
	}
	for(int i=0; i < count; i++){
		if(score[i] == -1){
			cout << -1 << endl;
			return 0;
		}
	}
	for(int i=0; i < count; i++){
		cout << score[i] << endl;
	}
}