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))
#define MIKU 39
using namespace std;
int n, w, dp[100000][393+1];

int p[MIKU*10+4], len[MIKU*10+4];
int rec(int i, int j){
  if(dp[i][j] >= 0) return dp[i][j];
  int res;
  if(i == MIKU*10+4 || j == 0) res = 0;
  else if(j < i) res = rec(i + 1, j);
  else  res = max(rec(i + 1, j), rec(i + 1, j - 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++) p[j] = max(p[j], 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<bits/stdc++.h>
using namespace std;
using Int = long long;
using ll = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n;
  cin>>n;
  vector<Int> ss(n),ls(n),ps(n);
  for(Int i=0;i<n;i++) cin>>ss[i]>>ls[i]>>ps[i];

  const Int MAX = 500;
  const Int INF = 1e15;
  vector<Int> dp(MAX,-INF);
  dp[0]=0;
  for(Int i=0;i<n;i++)
    for(Int j=0;j<MAX;j++)
      for(Int k=ss[i];k<=ls[i];k++)
        if(j+k<MAX) chmax(dp[j+k],dp[j]+ps[i]);    
  
  Int m;
  cin>>m;
  vector<Int> ws(m);
  for(Int i=0;i<m;i++) cin>>ws[i];  
  
  auto NG=[](){cout<<-1<<endl;exit(0);};
  for(Int i=0;i<m;i++)
    if(dp[ws[i]]<0) NG();
  
  for(Int i=0;i<m;i++) cout<<dp[ws[i]]<<endl;
  return 0;
}

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

using namespace std;

int main() {
	int n;

	cin >> n;

	vector< pair< int, pair< int, int > > > mel(n);

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

		mel[i].first = p;
		mel[i].second.first = s;
		mel[i].second.second = l;
	}

	int m;

	cin >> m;

	vector< vector< int > > dp(m, vector< int >(394, 0));
	vector< int > w(m);

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


	for(int i = 0; i < m; i++) {
		for(int j = 0; j < n; j++) {
			dp[i][mel[j].second.first] += mel[j].first;
			dp[i][mel[j].second.second] += mel[j].first;
		}
	}

	for(int i = 0; i < m; i++) {
		for(int j = 0; j <= 393; j++) {
			if(dp[i][j] != 0) {
				for(int k = 0; k < n; k++) {
					if(j + mel[k].second.first <= 393) {
						dp[i][j + mel[k].second.first] = max(dp[i][j + mel[k].second.first] ,dp[i][j] + mel[k].first);
					}
					if(j + mel[k].second.second <= 393) {
						dp[i][j + mel[k].second.second] = max(dp[i][j + mel[k].second.second] ,dp[i][j] + mel[k].first);
					}
				}
			}
		}
	}

	bool flag = true;

	for(int i = 0; i < m; i++) {
		if(dp[i][w[i]] == 0) {
			flag = false;
			break;
		}
	}

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

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);
	int M = -1;
	for (int i=0; i<m; i++) {
		cin >> W[i];
		M = max(M, 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 i=1; i*S[0]<=M; i++)
	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<=M; j++) {
			for (int k=S[i]; k<=L[i]; k++) {
				dp[i][j+k] = max(max(dp[i][j+k], dp[i-1][j+k]), dp[i-1][j]+P[i]);
			}
		}
	}

	vector<int> point(m);
	bool flag = false;
	for (int i=0; i<m; i++) {
		point[i] = 0;
		for (int j=0; j<n; j++)
			point[i] = max(point[i], dp[j][W[i]]);
		if (!point[i]) {
			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 <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);
	int M = -1;
	for (int i=0; i<m; i++) {
		cin >> W[i];
		M = max(M, 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 i=1; i*S[0]<=M; i++)
		for (int j=i*S[0]; j<=i*S[0]+SUB[0]; j++)
			dp[0][j] = max(dp[0][j], i*P[0]);
	
	for (int i=1; i<n; i++) {
		for (int j=0; j<=M; j++) {
			for (int k=S[i]; k<S[i]+SUB[i]; k++) {
				dp[i][j+k] = max(max(dp[i][j+k], dp[i-1][j+k]), dp[i-1][j]+P[i]);
			}
		}
	}

	vector<int> point(m);
	bool flag = false;
	for (int i=0; i<m; i++) {
		point[i] = 0;
		for (int j=0; j<n; j++)
			point[i] = max(point[i], dp[j][W[i]]);
		if (!point[i]) {
			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 <cstring>
#include <vector>
using namespace std;

int dp[400];
int n,s,l,p,w,ans[400];

int main(){
  //  pair<int ,int> P;


  vector<pair<int,int> > v;

  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+1)/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;
  }

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