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>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
  int n,m;
  int ph[394],br[394],mo[394];
  cin >>n;
  for(int i=1; i<=n; i++){
    cin >>ph[i]>>br[i]>>mo[i];
    br[i]-=ph[i];
  }
  vector<int> ans;
  int dp[394]={0};
  for(int i=1; i<=n; i++){
    for(int j=0; j<=393; j++){
      for(int k=0; k<=br[i]; k++){
	if(j-k-ph[i]>=0){
	  if(j-k-ph[i] == 0 || dp[j-k-ph[i]]!=0){
	    dp[j] = max(dp[j],dp[j-k-ph[i]]+mo[i]);
	  }
	}
      }
    }
  }
  cin >>m;
  for(int i=0,x; i<m; i++){
    cin >>x;
    if(dp[x] != 0) ans.push_back(dp[x]);
    else{cout <<"-1"<<endl;goto jump;}
  }
  for(int i=0; i<ans.size(); i++){
    cout <<ans[i]<<endl;
  }
 jump:
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

struct RangeSegmentTree
{
  vector< int > data;
  int sz;

  RangeSegmentTree(int n)
  {
    sz = 1;
    while(sz < n) sz <<= 1;
    data.assign(2 * sz - 1, -1);
  }

  void update(int a, int b, int x, int k, int l, int r)
  {
    if(a >= r || b <= l) {
      return;
    } else if(a <= l && r <= b) {
      data[k] = max(data[k], x);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) >> 1);
      update(a, b, x, 2 * k + 2, (l + r) >> 1, r);
    }
  }

  void update(int a, int b, int x)
  {
    update(a, b, x, 0, 0, sz);
  }

  int get(int k)
  {
    k += sz - 1;
    int ret = data[k];
    while(k > 0) {
      k = (k - 1) >> 1;
      ret = max(ret, data[k]);
    }
    return (ret);
  }
};

int main()
{
  int N, M;
  RangeSegmentTree tree(394);
  int dp[394];

  cin >> N;
  while(N--) {
    int s, l, p;
    cin >> s >> l >> p;
    tree.update(s, l + 1, p);
  }
  for(int i = 0; i < 394; i++) {
    dp[i] = tree.get(i);
  }

  for(int i = 0; i < 394; i++) {
    if(dp[i] == -1) continue;
    for(int j = 0; j <= i && i + j < 394; j++) {
      if(dp[j] == -1) continue;
      dp[i + j] = max(dp[i + j], dp[i] + dp[j]);
    }
  }
  cin >> M;
  vector< int > q;
  while(M--) {
    int w;
    cin >> w;
    if(~dp[w])  {
      q.emplace_back(dp[w]);
    } else {
      cout << -1 << endl;
      exit(0);
    }
  }
  for(int k : q) {
    cout << k << endl;
  }
}

C++: #include<iostream>
#include<vector>
#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=-1;
    }

};

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

    vector<int> ans;
    bool flag = false;
    
    rep(i,m) {
        int t = dp[w[i]];
        if(t < 0) flag=true;
        else ans.push_back(t);
    }
    if(flag) {
        cout<<"-1"<<endl;
        return 0;
    }

    rep(i,ans.size()) cout<<ans[i]<<endl;

    return 0;
}

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

using namespace std;

#define REP(i, n) for(int i=0;i<(int)n;i++)
#define rep(i, n) for(int i=0;i<(int)n;i++)
#define reep(i, s, t) for(int i=s;i<(int)t;i++)
#define irep(i, n) for(int i=(int)n-1;i>=0;i--)
#define ireep(i, n, s) for(int i=(int)n-1;i>=s;i--)
#define all(v) v.begin(), v.end()
#define vc vector
#define pb push_back

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n,m;
const int INF = 1<<25;
int dp[1005];
int main(){
	cin >> n;
	REP(i, 400) dp[i] = -INF;
	dp[0] = 0;
	REP(tttt, n){
		int s, t, w;
		cin >> s >> t >> w;
		rep(i, 400){
			for(int j=s;j<=t;j++) dp[i+j] = max(dp[i+j], dp[i] + w);
		}
	}
	vi ans;
	int f = 1;
	cin >> m;
	REP(i, m){
		int x;
		cin >> x;
		ans.pb(dp[x]);
		if(dp[x] < 0) f = 0;
	}
	if(!f) cout << -1 << endl;
	else{
		for(int v : ans) cout << v << endl;
	}
	return 0;
}

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

int dp[400][400];
int main() {
    int N; cin >> N;
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for(int i=0; i<N; i++) {
        int s, l, p; cin >> s >> l >> p;
        for(int j=0; j<400; j++) {
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            for(int k=s; k<=l; k++) {
                if(j - k < 0) continue;
                if(dp[i][j - k] < 0) continue;
                dp[i+1][j] = max(dp[i+1][j], dp[i][j - k] + p);
            }
        }
    }
    
    int M; cin >> M;
    bool no_answer = false;

    vector<int> rec(M);
    for(int i=0; i<M; i++) {
        int v; cin >> v;
        rec[i] = v;
        no_answer |= (dp[N][v] < 0); 
    }

    if(no_answer) cout << -1 << endl;
    else {
      for(int i=0; i<M; i++) {
        cout << dp[N][ rec[i] ] << endl;
      }
    }
    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;
}