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

C++: #include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
//#include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)

int N,s,l,p;
int dp[401];
const int Z=400;
int M;
priority_queue<P> q;
int x[400];
int main()
{
	cin>>N;
	rep(i,Z+1) dp[i]=-1;
	dp[0]=0; 
	rep(i,N){
		cin>>s>>l>>p;
		int lmax=0;
		for(int j=s;j<Z+1;j++){
			if(dp[j-s]>=0) q.push( P(dp[j-s] , j-s ) );
			while(!q.empty() && j>l && q.top().sc < j-l) q.pop();
			//cout<<j<<" "<<Z<<endl;
			if(q.top().fr==-1) continue;
			lmax=q.top().fr;
			dp[j]=max( lmax+p ,dp[j] );
			
		}
		while(!q.empty()) q.pop();
	}
	cin>>M;
	bool b=0;
	rep(i,M){
		cin>>x[i];
		if(dp[x[i]]==-1){
			puts("-1");
			return 0;
		}
	}
	rep(i,M) cout<<dp[x[i]]<<endl;//<<endl;
	//rep(i,10) cout<<dp[i]<<endl;
}

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))
using namespace std;
int n, w;
vector<int>p, len;

int rec(int i, int j){
  int res;
  if(i == p.size() || j == 0) res = 0;
  else if(j < len[i]) res = rec(i + 1, j);
  else  res = max(rec(i + 1, j), rec(i + 1, j - len[i]) + p[i]);

  return res;
}

int main(){
  int n, m, s, l, point;
  scanf("%d", &n);
  rep(i, n){
    scanf("%d%d%d", &s, &l, &point);
    for(int j = s; j <= l; j++){ len.push_back(j); p.push_back(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;
}