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++: //============================================================================
// Name        : aoj2502.cpp
// Author      : afterCmidday
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int n, s[393], l[393], p[393], m, w[393], dp[394];
	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[i];
	}
	fill(dp,dp + 394,-1);
	dp[0] = 0;
	for(int i = 0;i <= 393;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 - k] + p[j], dp[i]);
				}
			}
		}
	}
	for(int i = 0;i < m;i++){
		if(dp[w[i]] == -1){
			cout << -1 << endl;
			return 0;
		}
	}
	for(int i = 0;i < m;i++){
		cout << dp[w[i]] << 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 394
using namespace std;

int main(){
  int n,s[N],l[N],p[N],dp[N][N],m,w,ans[N],flag=0;
  cin>>n;
  for(int i=0;i<n;i++) cin>>s[i]>>l[i]>>p[i];
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++) dp[i][j]=-1;
  dp[0][0]=0;
  for(int i=0;i<N;i++)
    for(int j=s[0];j<=l[0];j++){
      if(i-j>=0&&dp[0][i-j]!=-1) dp[0][i]=dp[0][i-j]+p[0];
    }
  for(int i=1;i<n;i++){
    for(int j=0;j<N;j++) {
      for(int k=s[i];k<=l[i];k++){
	if(j-k>=0&&dp[i][j-k]!=-1)dp[i][j]=max(dp[i][j],dp[i-1][j-k]+p[i]);
      }
      dp[i][j]=max(dp[i][j],dp[i-1][j]);
    }
  }
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    if(dp[n-1][w]==-1){
      flag=1;
      break;
    }else ans[i]=dp[n-1][w];
  }
  if(!flag)
    for(int i=0;i<m;i++) cout<<ans[i]<<endl;
  else cout<<-1<<endl;
  return 0;
}

C++: #include<cstdio>
#include<bits/stdc++.h>
typedef long long int ll;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for(int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))
#define MAX 393
using namespace std;

/*
n
s1 l1 p1
...
sN lN pN
m
w1
...
wm
 */

int n,m;
int dp[MAX+1][MAX+1];

int main(){
  cin >> n;

  REP(j,MAX+1){
    REP(k,MAX+1){
      dp[0][j] = j==0 ? 0 : -1;
    }    
  }

  REP(i,n){
    int s,l,p;
    cin >> s >> l >> p;
    REP(j,MAX+1){
      int dp1 = -1;
      FOR_IN(w,s,l+1)
        dp1 = j>=w && dp[i+1][j-w]!=-1 ? max(dp1, dp[i+1][j-w] + p) : dp1;
      dp[i+1][j] = max(dp[i][j], dp1);
    }
  }

  cin >> m;
  int w[m];
  bool f=true;
  REP(r,m){
    cin >> w[r];
    f &= dp[n][w[r]] != -1;
  }

  if(!f)
    cout << -1 << endl;
  else
    REP(r,m)
      cout << dp[n][w[r]] << endl;

  return 0;
}

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

using namespace std;

int MAX = 393;

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

	vector<int> S(n), L(n), P(n);//, SUB(n);
	for (int i=0; i<n; i++) {
		cin >> S[i] >> L[i] >> P[i];
		//SUB[i] = L[i]-S[i];
	}

	int m;
	cin >> m;

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

	int **dp = new int*[MAX+10];
	for (int i=0; i<MAX+10; i++) {
		dp[i] = new int[3*MAX];
		for (int j=0; j<3*MAX; j++)
			dp[i][j] = 0;
	}

	dp[0][0] = 0;
	for (int j=S[0]; j<=L[0]; j++)
		dp[0][j] = max(dp[0][j], P[0]);
	
	for (int i=1; i<n; i++) {
		for (int j=0; j<=MAX; j++) {
			if (j < S[i]) {
				dp[i][j] = dp[i-1][j];
			} else {
				for (int k=S[i]; k<=L[i]; k++) {
					if (j-S[i]>=0) dp[i][j] = max(max(dp[i][j], dp[i-1][j]), dp[i][j-k]+P[i]);
				}
			}
		}
	}

	vector<int> point(m);
	bool flag = false;
	for (int i=0; i<m; i++) {
		point[i] = dp[n-1][W[i]];
		if (point[i]==0) {
			flag = true;
			break;
		}
	}
	
	if (flag) cout << -1 << endl;
	else {
		for (int i=0; i<m; i++)
			cout << point[i] << endl;
	}

	for (int i=0; i<MAX+10; i++)
		delete [] dp[i];
	delete [] dp;

	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

int dp[400];

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];
	}
	for(int i=1; i<=393; i++) {
		rep(j, n) {
			for(int k=i-l[j]; k<=i-s[j]; k++) {
				if(k<0) continue;
				dp[i] = max(dp[i], dp[k] + p[j]);
			}
		}
	}
	bool fail = false;
	rep(i, m) {
		if( dp[w[i]] == 0 ) {
			fail = true;
		}
	}
	if(fail) cout << "-1" << endl;
	else {
		rep(i, m) {
			cout << dp[w[i]] << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[1000], a[1000];
int l[1000], r[1000], m[1000];
int N, Q;
int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> l[i] >> r[i] >> m[i];
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < 400; j++) {
			for (int k = l[i]; k <= r[i]; k++) {
				dp[j + k] = max(dp[j + k], dp[j] + m[i]);
			}
		}
	}
	cin >> Q; int OK = 0;
	for (int i = 0; i < Q; i++) {
		cin >> a[i]; 
		if (dp[a[i]] == 0) { OK = 1; }
	}
	if (OK == 1) { cout << "-1" << endl; }
	else {
		for (int i = 0; i < Q; i++) { cout << dp[a[i]] << endl; }
	}
	return 0;
}