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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;
int n, w, dp[1000][1000];
vector<int>p, len;

int rec(int i, int j){
  if(dp[i][j] >= 0) return dp[i][j];
  int res;
  if(i == p.size() || j == 0) res = 0;
  else if(j < len[i]) res = rec(i + 1, j);
  else  res = max(rec(i + 1, j), rec(i + 1, j - len[i]) + p[i]);

  return dp[i][j] = res;
}

int main(){
  int n, m, s, l, point;
  memset(dp, -1, sizeof(dp));
  scanf("%d", &n);
  rep(i, n){
    scanf("%d%d%d", &s, &l, &point);
    for(int j = s; j <= l; j++){ len.push_back(j); p.push_back(point);}
  }
  scanf("%d", &m);
  vector<int>res;
  rep(i, m){
    scanf("%d", &w);
    res.push_back(rec(0, w));
  }
  bool f = true;
  rep(i, res.size()) if(res[i] == 0) f = false;
  if(f) rep(i, res.size()) printf("%d\n", res[i]);
  else puts("-1");
  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=0;i<n;i++)
    for(int j=s[i];j<=l[i];j++){
      if(len+j>w) continue;
      flag2=1;
      func(x+1,len+j,point+p[i]);
    }
}

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

using namespace std;

int main(void){

  int n,m;

  cin >> n;

  vector<int>s(n),l(n),p(n);

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

  cin >> m;
  vector<int>w(m);

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

  int dp[400];
  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]);
      }
    }
  }
  
  for(int i=0;i<m;i++){
    if(dp[w[i]]<0){
      cout << -1 << endl;
      return 0;
    }
  }

  for(int i=0;i<m;i++)
    cout << dp[w[i]] << endl;

  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <complex>
#define repi(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define endl "\n"
#define ing long long
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2>& p) { os << p.first << " " << p.second; return os; }
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) { for(int i = 0; i < (int) v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; }

#define MAX 400

#define s first.first
#define l first.second
#define p second

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    vector<int> w;
    vector<pair<pair<int,int>,int>> in;
    vector<vector<int>> dp;
    
    cin>>n;

    in.resize(n);

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

    cin>>m;
    
    w.resize(m);

    dp.resize(2,vector<int>(MAX*MAX,0));

    dp[0][0] = 0;
    
    for(int i = 0; i < n; i++){
        for(int j = 0; j < MAX; j++){
            for(int k = in[i].s; k <= in[i].l ; k++){
                int num = j + k;
                if(num < MAX) dp[0][num] = max(dp[0][num], dp[0][j] + in[i].p);

                //dp[0][num] = max(dp[0][num], dp[0][num]);
            }
        }
    }


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

        if(dp[0][w[i]] == 0){

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

    return 0;
}

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

int main()
{
  int s[400];
  int l[400];
  int p[400];
  int w[400];

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

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

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

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

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


}

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

using namespace std;

int main(void){

  int n,m;

  cin >> n;

  vector<int>s(n),l(n),p(n);

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

  cin >> m;
  vector<int>w(m);

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

  int dp[400];
  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]);
      }
    }
  }
  
  for(int i=0;i<m;i++){
    if(dp[w[i]<0]){
      cout << -1 << endl;
      return 0;
    }
  }

  for(int i=0;i<m;i++)
    cout << dp[w[i]] << endl;

  return 0;
}

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

using namespace std;

int dp[400];

int main(int argc, char const* argv[])
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
			int s, l, p;
			cin >> s >> l >> p;
			for(int j = s; j < 394; j++){
					for(int k = s; k < l; k++){
							if(j - k < 0)continue;
							dp[j] = max(dp[j], dp[j - k] + p);
					}
			}
	}

	int m;
	cin >> m;
	vector<int> vec;
	for(int i = 0; i < m; i++){
			int w;
			cin >> w;
			if(dp[w] != 0){
					vec.push_back(dp[w]);
			}else{
					cout << -1 << endl;
					return 0;
			}
	}
	for(auto itr = vec.begin(); itr != vec.end(); ++itr){
			cout << *itr << endl;
	}
	return 0;
}