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

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

using namespace std;

int main() {
	int n;

	cin >> n;

	vector< int > s, l, p;

	for(int i = 0; i < n; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		s.push_back(a);
		l.push_back(b);
		p.push_back(c);
	}

	vector< int > dp(394, 0);
	for(int i = 0; i < n; i++) {
		dp[s[i]] = p[i];
		dp[l[i]] = p[i];
	}

	int m;
	cin >> m;
	vector< int > w;

	for(int i = 0; i < m; i++) {
		int tmp;
		cin >> tmp;
		w.push_back(tmp);
	}

	for(int i = 0; i < m; i++) {
		bool flag = true;
		for(int j = 1; j <= 393; j++) {
			for(int k = 0; k < n; k++) {
				dp[j + s[k]] = max(dp[j + s[k]], dp[j] + p[k]);
				dp[j + l[k]] = max(dp[j + l[k]], dp[j] + p[k]);
				if(j + s[k] > w[i]) {
					flag = false;
					break;
				}
			}
			if(!flag) {
				break;
			}
		}
	}

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

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

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
const ll INF = 9999999999;
const int INFI=2147483647;
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;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
  T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }


int main() {
  int n; cin >>n;
  vector<int> s(n),l(n),p(n);
  rep(i, n) cin >>s.at(i) >>l.at(i) >>p.at(i);
  int m; cin >>m;
  vector<int> w(m);
  rep(i, m) cin >>w.at(i);
  vector<int> res(m);
  bool ok = true;

  vector<int> dp;

  for (int melody=0; melody<m; melody++){
    int W = w.at(melody);
    dp.assign(W+1, 0);
    // dp[i]...長さiで得られる最高得点
    for (int i=0; i<n; i++){
      if (i>0) if (s.at(i) == s.at(i-1)) if (l.at(i) == l.at(i-1)) if (p.at(i) == p.at(i-1)) continue;
      for (int j=0; j<=W; j++){
        for (int k=s.at(i); k<=l.at(i); k++){
          if (j+k <= W) chmax(dp[j+k], dp[j] + p.at(i));
          else break;
        }
      }
    }
    res.at(melody) = dp[W];
    if (dp[W] == 0) ok = false;
  }

  if (ok) for (int melody=0; melody<m; melody++) cout <<res.at(melody) <<endl;
  else cout <<-1 <<endl;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;
 
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define rep(i,N) for(int i=0; i < (int)N; i++)
#define all(v) (v).begin(),(v).end()
//#define max(a,b) (a>b ? a : b)
//#define min(a,b) (a<b ? a : b)
 
const int MOD = 998244353;
const int INF = 100000002;
const double PI =4*atan(1);
const double eps = 1e-7;
const long long oo = 1e18;

int solver(const int N, const int M, vector<int> &S, vector<int> &L, vector<int> &P){
  vector<vl> dp(N+1, vl((M+1),0));

  rep(i,N){
    rep(j,M+1){
      if(j<S[i]){
        dp[i+1][j] = max(dp[i][j],dp[i+1][j]);
        continue;
      }else{
        dp[i+1][j] = max({dp[i][j],dp[i+1][j-S[i]]+P[i],dp[i+1][max(0,j-L[i])]+P[i]});
      } 
    } 
  }
  return dp[N][M];
}
  


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;
  bool check=false;
  vector<int> ans(M);
  rep(i,M){
    int m;
    cin >> m;
    int a = solver(N,m,S,L,P);
    if(a<=0){
      cout << -1 << endl;
      return 0;
    }else ans[i]=a;
  }
  rep(i,M){
    cout << ans[i] << endl;
  }
}