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 <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[160001];
const int Z=160000;
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<algorithm>

using namespace std;

int main(void){
  int n,m,s[400],l[400],p[400],w[400],dp[400];
  
  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+400,-1);
  dp[0]=0;

  for(int i=1;i<400;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],dp[i-k]+p[j]);
  
  for(int i=0;i<m;i++){
    if(dp[w[i]]<0){
      cout << -1 << endl;
      return 0;
    }
  }
    
  for(int i=0;i<m;i++)
    cout << dp[w[i]] << endl;
  
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int dp[394][394];
int l[393];
int r[393];
int p[393];
int ans[393];
int main(){
	int a;
	scanf("%d",&a);
	for(int i=0;i<a;i++){
		scanf("%d%d%d",l+i,r+i,p+i);
	}
	for(int i=0;i<394;i++)
		for(int j=0;j<394;j++)
			dp[i][j]=-99999999;
	dp[0][0]=0;
	for(int i=1;i<=a;i++){
		for(int j=0;j<394;j++){
			dp[i][j]=max(dp[i][j],dp[i-1][j]);
			for(int k=l[i-1];k<=r[i-1];k++){
				if(j-k>=0){
					dp[i][j]=max(dp[i][j],dp[i-1][j-k]+p[i-1]);
					dp[i][j]=max(dp[i][j],dp[i][j-k]+p[i-1]);
				}
			}
		}
	}
	int b;
	bool ok=true;
	scanf("%d",&b);
	for(int i=0;i<b;i++){
		int c;
		scanf("%d",&c);
		ans[i]=dp[a][c];
		if(ans[i]<0)ok=false;
	}
	if(ok){
		for(int i=0;i<b;i++)printf("%d\n",ans[i]);
	}else printf("-1\n");
	
}

C++: #include<iostream>
#include<vector>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class Phrase {
    public:
    int s,l,p;
    Phrase(int s,int l,int p) {

        this->s = s;
        this->l = l;
        this->p = p;
    }
    Phrase() {
        s=l=p=-1;
    }

};

int n;
Phrase phra[400];
int m;
int w[400];
int dp[400];

int main() {

    cin>>n;
    rep(i,n) {
        int s,l,p;
        cin>>s>>l>>p;
        phra[i] = Phrase(s,l,p);
    }
    cin>>m;
    rep(i,m) cin>>w[i];
    rep(i,400) dp[i]=-1;
    dp[0]=0;

    rep(i,n) {
        for(int j=phra[i].s;j<=phra[i].l;j++) {
            for(int k=j;k<400;k++) {
                dp[k] = max(dp[k],dp[k-j]+phra[i].p);
            }
        }
    }

    vector<int> ans;
    bool flag = false;
    
    rep(i,m) {
        int t = dp[w[i]];
        if(t < 0) flag=true;
        else ans.push_back(t);
    }
    if(flag) {
        cout<<"-1"<<endl;
        return 0;
    }

    rep(i,ans.size()) cout<<ans[i]<<endl;

    return 0;
}

C++: #include<iostream>
#include<set>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class Phrase {
    public:
    int s,l,p;
    Phrase(int s,int l,int p) {

        this->s = s;
        this->l = l;
        this->p = p;
    }
    Phrase() {
        s=l=p=0;
    }

};

int n;
Phrase phra[400];
int m;
int w[400];
int dp[400];

int main() {

    cin>>n;
    rep(i,n) {
        int s,l,p;
        cin>>s>>l>>p;
        phra[i] = Phrase(s,l,p);
    }
    cin>>m;
    rep(i,m) cin>>w[i];

for(int k=0;k<400;k+=j) {
    rep(i,n) {
        for(int j=phra[i].s;j<=phra[i].l;j++) {
            
                dp[k+j] = max(dp[k+j],dp[k]+phra[i].p);
            }
        }
    }

    multiset<int> ans;
    
    rep(i,m) ans.insert(dp[w[i]]);    
    if(ans.find(0) != ans.end()) {
        cout<<"-1"<<endl;
        return 0;
    }

    multiset<int>::iterator it = ans.begin();
    while(it != ans.end()) {
        cout<<(*it)<<endl;
        it++;
    }




    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

int main(){
	int n, m;
	int s[400], l[400], p[400];
	int w[400];
	int dp[400];

	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+400, -1);
	dp[0] = 0;

	for(int i=0; i < 400; 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], dp[i-k] + p[j]);
			}
		}
	}

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

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;

int s[500],l[500],p[500];
int w[500];
int dp[500];

int main() {
  int n;
  while(cin >> n) {
    REP(i,n) {
      cin >> s[i] >> l[i] >> p[i];
    }
    memset(dp,-1,sizeof(dp));
    dp[0] = 0;
    REP(i,n) {
      for (int j=s[i]; j<=l[i]; ++j) {
        REP(k,400-j) {
          if (dp[k] == -1) continue;
          chmax(dp[j+k], dp[k] + p[i]);
        }
      }
    }
//    REP(i,10) cout << dp[i] << " " ; cout << endl;
    int m; cin >> m;
    vector<int> ans;
    bool flag = 0;
    REP(i,m) {
      int w; cin >> w;
      if (dp[w] == -1) flag = 1;
      ans.push_back(dp[w]);
    }
    if (flag) cout << -1 << endl;
    else {
      FOR(it, ans) cout << *it << endl;
    }
  }
}