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 <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
using ll = long long;
using namespace std;

int s[401], l[401], p[401], w[401];

int dp[401][401]; //indexの番号 重さ 最大コストをもつ
int const MOD = 1e9 + 7;
int main(void)
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> s[i] >> l[i] >> p[i];

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

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= 393; ++j)
        {
            for (int k = s[i]; k <= l[i]; ++k)
            {
                if (j - k >= 0)
                {
                    dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - k] + p[i]);
                }
                else
                {
                    dp[i + 1][j] = dp[i][j];
                }
            }
        }
    }

    bool ok = true;
    vector<int> ans;
    for (int i = 0; i < m; ++i)
    {
        if (dp[n][w[i]] == 0)
            ok = false;
        ans.push_back(dp[n][w[i]]);
    }
    if (!ok)
        cout << -1 << endl;
    else
    {
        for (auto e : ans)
            cout << e << endl;
    }
    return 0;
}

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";
      f = 1;
    }
    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 <algorithm>
#include <iostream>
#define N 393
using namespace std;
void func(int,int,int);
int n,s[N],l[N],p[N],m,w,a,ans[N],flag,flag2;

int main(){
  cin>>n;
  for(int i=0;i<n;i++) cin>>s[i]>>l[i]>>p[i];
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    a=-1;
    flag2=0;
    func(0,0,0);
    if(!flag2) a=-1;
    if(a==-1) flag=1;
    else ans[i]=a;
  }
  if(flag) cout<<-1<<endl;
  else{
    for(int i=0;i<m;i++) cout<<ans[i]<<endl;
  }
  return 0;
}

void func(int x,int len,int point){
  if(x==n){
    a=max(a,point);
    return ;
  }
  func(x+1,len,point);
  for(int i=s[x];i<=l[x];i++){
    if(len+i>w) continue;
    flag2=1;
    func(x+1,len+i,point+p[x]);
  }
}

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

int main(void)
{
	int n, s[393], l[393],p[393];
	cin >> n;
	for(int i = 0; i < n; i++) cin >> s[i] >> l[i] >> p[i];
	int m, w[393];
	cin >> m;
	int longest = 0;
	for(int i = 0; i < m; i++){
		cin >> w[i];
		longest = max(longest,w[i]);
	}
	
	int point[394][394] = {0};
	for(int j = 0; j < n; j++){//j番目のフレーズ
		for(int k = 0; k < longest; k++) point[j + 1][k] = point[j][k];

		for(int k = 0; k < longest; k++){
			if(k == 0 || point[j][k]){
				for(int x = s[j]; x <= l[j]; x++){
					if(k + x <= longest){
						point[j + 1][k + x] = max(point[j + 1][k + x], point[j][k] + p[j]);
					}
				}
			}
		}
	}

	int max[393];
	for(int i = 0; i < m; i++){//i番目のメロディー
		if(point[n][w[i]]) max[i] = point[n][w[i]];
		else {max[0] = -1; break;}
	}


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

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for(ll i=0; i<n; i++)
#define Rep(i, j, n) for(ll i=j; i<n; i++)
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmin(T& a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if(a < b) {a = b; return true;} return false;}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

int dp[1000];

int main() {
    int n; cin >> n;
    rep(i, 1000) dp[i] = 0;
    rep(i, n) {
        int s, l, p; cin >> s >> l >> p;
        Rep(j, s, l+1) rep(k, 400) chmax(dp[k+j], dp[k] + p);
    }
    int m; cin >> m;
    vector<int> me(m);
    rep(i, m) cin >> me[i];
    rep(i, m) if(dp[me[i]] == 0) {
        cout << -1 << endl;
        return 0;
    }
    rep(i, m) cout << dp[me[i]] << endl;
}

C++: /*
 * 2502.cc: VOCAL ANDROID
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 393;
const int MAX_M = 393;
const int MAX_W = 393;

/* typedef */

typedef vector<int> vi;

/* global variables */

int ss[MAX_N + 1], ls[MAX_N + 1], ps[MAX_N + 1];
int dp[MAX_W + 1];
vi wis;

/* subroutines */

/* main */

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

  for (int i = 0; i < n; i++) cin >> ss[i] >> ls[i] >> ps[i];

  int m;
  cin >> m;

  bool ok = true;

  while (m--) {
    int w;
    cin >> w;

    memset(dp, -1, sizeof(dp));
    dp[0] = 0;

    for (int i = 0; i < n; i++)
      for (int j = w - ss[i]; j >= 0; j--)
	if (dp[j] >= 0)
	  for (int k = ss[i]; k <= ls[i]; k++) {
	    int j0 = j + k, dp0 = dp[j] + ps[i];
	    if (j0 <= w && dp[j0] < dp0) dp[j0] = dp0;
	  }

    int maxdp = 0;
    for (int i = 0; i <= w; i++)
      if (maxdp < dp[i]) maxdp = dp[i];
    wis.push_back(maxdp);
    if (maxdp == 0) ok = false;
  }

  if (ok)
    for (int i = 0; i < wis.size(); i++) printf("%d\n", wis[i]);
  else
    puts("-1");
  
  return 0;
}