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 main()
{
    int N,M,x;
    cin >> N;
    int s[N],l[N],p[N];
    for(int i = 0 ; i < N ; i++){
        cin >> s[i] >> l[i] >> p[i];
    }
    int dp[500] = {0};
    for(int i = 0 ; i < N ; i++){
        for(int j = 0 ; j < 500 ; j++){
            for(int k = s[i] ; k <= l[i] ; k++){
                if(j + k >= 500) continue;
                dp[j+k] = max(dp[j+k],dp[j]+p[i]);
            }   
        }
    }
    cin >> M;
    vector<int> res(M);
    bool check = 0;
    for(int i = 0 ; i < M ; i++){
        cin >> x;
        res[i] = dp[x];
        if(res[i] == 0) check = 1;
    }
    if(check){
        cout << -1 << endl;
    }else{
        for(auto v : res){
            cout << v << endl;
        }
    }   
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> s(N), l(N), p(N);
    for (int i = 0; i < N; i++) {
        cin >> s[i] >> l[i] >> p[i];
    }

    vector<vector<int> > dp(N+1, vector<int>(400, -1));
    dp[0][0] = 0;

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 400; j++) {
            if (dp[i][j] < 0) continue;
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            for (int k = s[i]; k <= l[i]; k++) {
                if (j+k < 400)
                    dp[i+1][j+k] = max(dp[i+1][j+k], dp[i][j] + p[i]);
            }
        }
    }

    int M;
    cin >> M;

    vector<int> m(M);
    bool flag = true;
    for (int i = 0; i < M; i++) {
        cin >> m[i];
        if (dp[N][m[i]] < 0) flag = false;
    }
    if (flag) {
        for (auto i : m) cout << dp[N][i] << endl;
    }
    else {
        cout << -1 << endl;
    }

    return 0;
}

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

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 */

vi cs, ps, ans;
int wis[MAX_M + 1], dp[2][MAX_W + 1];

/* subroutines */

/* main */

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

  for (int i = 0; i < n; i++) {
    int si, li, pi;
    cin >> si >> li >> pi;
    for (int j = si; j <= li; j++)
      cs.push_back(j), ps.push_back(pi);
  }
  n = cs.size();
  
  int m, maxw = 0;
  cin >> m;

  for (int i = 0; i < m; i++) {
    cin >> wis[i];
    if (maxw < wis[i]) maxw = wis[i];
  }

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

  for (int i = 0; i < n; i++) {
    memcpy(dp[nxt], dp[cur], sizeof(dp[0]));

    int &ci = cs[i], c = ci;
    int &pi = ps[i], p = pi;

    while (c <= maxw) {
      int max_j = maxw - c;
      for (int j = 0; j <= max_j; j++)
        if (dp[cur][j] >= 0) {
          int d = dp[cur][j] + p;
          if (dp[nxt][j + c] < d) dp[nxt][j + c] = d;
        }
      c += ci;
      p += pi;
    }
    cur ^= 1; nxt ^= 1;
  }

  bool ok = true;
  
  for (int i = 0; i < m; i++)
    if (dp[cur][wis[i]] <= 0) {
      ok = false;
      break;
    }

  if (ok)
    for (int i = 0; i < m; i++) printf("%d\n", dp[cur][wis[i]]);
  else
    puts("-1");

  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

int main()
{
	for(int n,m;cin>>n;){
		vi ss(n),ls(n),ps(n);
		rep(i,n) cin>>ss[i]>>ls[i]>>ps[i];
		cin>>m;
		vi ws(m);
		rep(i,m) cin>>ws[i];
		
		vi dp(394,-1); dp[0]=0;
		rep(i,n) repi(j,ss[i],ls[i]+1) repi(k,j,394){
			if(dp[k-j]==-1) continue;
			dp[k]=max(dp[k],dp[k-j]+ps[i]);
		}
		
		vi res;
		for(int w:ws){
			if(dp[w]==-1){res.assign(1,-1); break;}
			else          res.push_back(dp[w]);
		}
		rep(i,res.size())
			cout<<res[i]<<endl;
	}
	
	return 0;
}