Go: ///
// File:  2502.go
// Author: ymiyamoto
//
// Created on Sat Mar 24 00:44:45 2018
//
package main

import (
	"fmt"
)

var n, m int

type phrase struct {
	s, l, p 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, max_count+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 <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;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int N = 400;
const int INF = 19191919;
int dp[N];

int main(){
    int n;
    cin >>n;
    vector<int> s(n),l(n),p(n);
    rep(i,n) cin >>s[i] >>l[i] >>p[i];

    int m;
    cin >>m;
    vector<int> w(m);
    rep(i,m) cin >>w[i];

    rep(i,N) dp[i] = -INF;
    dp[0] = 0;

    rep(i,N){
        if(dp[i] < 0) continue;

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

    vector<int> a(m);
    rep(i,m){
        int ans = dp[w[i]];
        if(ans<0){
            cout << -1 << "\n";
            return 0;
        }
        a[i] = ans;
    }

    rep(i,m) cout << a[i] << "\n";
    return 0;
}

C++: #include <iostream>
#include <cstring>

using namespace std;

int main(){
  int n;
  cin>> n;
  int d[400],u[400],s[400];
  for(int i=0;i<n;i++){
    cin>>d[i]>>u[i]>>s[i];
  }
  int dp[400]={};
  memset(dp,-1,sizeof(dp));
  dp[0]=0;

  for(int i=0;i<n;i++)
    for(int j=399;j>=0;j--)
      for(int k=u[i];k>=d[i];k--)
	if(j+k<400 && dp[j]!=-1) dp[j+k] = max(dp[j+k],dp[j]+s[i]);
      


  int q,flg=0,m[400];
  cin>> q;
  for(int i=0;i<q;i++){
    cin >> m[i];
    if(dp[m[i]] == -1) flg= 1;
  }
  if(flg) cout << -1<<endl;
  else 
    for(int i=0;i<q;i++) cout << dp[m[i]]<<endl;
  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

int main(){

	int N,M,left,right,point,dp[394];
	int words[394];

	for(int i = 1; i <= 393; i++)words[i] = 0;

	scanf("%d",&N);

	for(int loop = 0; loop < N; loop++){
		scanf("%d %d %d",&left,&right,&point);
		for(int i = left; i <= right; i++){
			words[i] = max(words[i],point);
		}
	}

	bool FLG = true;
	queue<int> Q;
	int length;

	scanf("%d",&M);

	for(int loop = 0; loop < M; loop++){
		scanf("%d",&length);

		for(int i = 0; i <= length; i++)dp[i] = 0;

		for(int i = 1; i <= length; i++){
			for(int loc = i; loc <= length; loc++){
				if(loc-i == 0 || dp[loc-i]  != 0){
					dp[loc] = max(dp[loc],dp[loc-i]+words[i]);
				}
			}
		}

		if(dp[length] == 0){
			FLG = false;
		}else{
			Q.push(dp[length]);
		}
	}

	if(!FLG){
		printf("-1\n");
	}else{
		while(!Q.empty()){
			printf("%d\n",Q.front());
			Q.pop();
		}
	}

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