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++: //b
#include <iostream>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <memory>
#include <functional>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
 
#define REP(i,b,n) for(int i=b;i<n;i++)
#define REPR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define EACH(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n>>b) & 1)
#define PB push_back
#define MP make_pair
 
using namespace std;
 
static const double PI = acos(-1.0);
static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> pii;
 
int n;
int s[400], l[400], p[400];
int seg = 0;
int m;
int w[400];
queue<int> res;
 
int dp[400][400];
 
bool solve(){
     
    for(int k=0; k<m; ++k){
         
        CLR(dp);
         
        for(int i=0; i<n; ++i){
            for(int j=0; j<=w[k]; ++j){
             
                    if(j<s[i])
                        dp[i+1][j] = dp[i][j];
                    else
                        dp[i+1][j] = max(dp[i][j], dp[i+1][j-s[i]] + p[i]);
            }
        }
         
         
        int ret = 0;
         
        for(int i=w[k]; i>=w[k]-seg; --i){
            if(dp[n][i] != 0)
                ret = max(ret, dp[n][i]);
        }
         
        if(ret == 0)
            return false;
         
        res.push(ret);
    }
     
    return true;
}
 
int main(){
     
    cin >> n;
     
    for(int i=0; i<n; ++i){
        cin >> s[i] >> l[i] >> p[i];
        seg += l[i]-s[i];
    }
     
    cin >> m;
     
    for(int i=0; i<m; ++i){
        cin >> w[i];
    }
     
    if(solve()==false)
        cout << -1 << endl;
    else{
        while(!res.empty()){
            cout << res.front() << endl;
            res.pop();
        }
    }
     
    return 0;
}

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

#define int             long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )    for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )  FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP              make_pair

using namespace std;
using P = pair<int, int>;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

struct Melody{
    int low, high, point;
};

int dp[400];

signed main(){
    int N; cin >> N;
    vector<Melody> melody( N );
    REP( i, N ){
        cin >> melody[i].low >> melody[i].high >> melody[i].point;
    }

    REP( i, N ){
        REP( j, 400 ){
            FOR( k, melody[i].low, melody[i].high + 1 ){
                if( j >= k ){
                    dp[j] = max( dp[j], dp[j - k] + melody[i].point );
                }
            }
        }
    }

    int M; cin >> M;
    vector<int> ans;
    REP( i, M ){
        int w; cin >> w;
        if( !dp[w] ) {
            cout << -1 << endl;
            return 0;
        }
        ans.push_back( dp[w] );
    }

    for( auto v : ans ){
        cout << v << endl;
    }

}

C++: #include<bits/stdc++.h>
using namespace std;int dp[999],a[999],l[999],r[999],m[999],N,Q,i,j,k,OK;main(){cin>>N;for(i=0;i<N;i++)cin>>l[i]>>r[i]>>m[i];for(i=0;i<N;i++)for(j=0;j<400;j++)for(k=l[i];k<=r[i];k++)dp[j+k]=max(dp[j+k],dp[j]+m[i]);cin>>Q;for(i=0;i<Q;i++){cin>>a[i];OK=dp[a[i]]?OK:1;}if(OK)cout<<"-1\n";else for(i=0;i<Q;i++)cout<<dp[a[i]]<<endl;}

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

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

C++: #include <bits/stdc++.h>
#define INF 1000000000000000001
#define ll long long
#define pll pair<ll, ll>
using namespace std;


int main()
{
  ll N;
  cin >> N;
  vector<tuple<ll, ll, ll>> slp(N);
  for (ll i = 0; i < N; ++i)
  {
    ll s, l, p;
    cin >> s >> l >> p;
    slp.at(i) = make_tuple(s, l, p);
  }
  vector<ll> dp(394, -1);
  dp.at(0) = 0;
  for (ll i = 0; i < 394; ++i)
  {
    for (ll j = 0; j < N; ++j)
    {
      if (dp.at(i) == -1)
      {
        continue;
      }
      ll s = get<0>(slp.at(j)), l = get<1>(slp.at(j)), p = get<2>(slp.at(j));
      for (ll k = s; k < l + 1; ++k)
      {
        if (i + k > 393)
        {
          break;
        }
        dp.at(i + k) = max(dp.at(i + k), dp.at(i) + p);
      }
    }
  }
  ll m;
  cin >> m;
  bool wrong = false;
  vector<ll> ws(m);
  for (ll i = 0; i < m; ++i)
  {
    ll w;
    cin >> w;
    ws.at(i) = w;
    if (dp.at(w) == -1)
    {
      wrong = true;
    }
  }
  if (wrong)
  {
    cout << -1 << endl;
  }
  else
  {
    for (ll i = 0; i < m; ++i)
    {
      cout << dp.at(ws.at(i)) << endl;
    }
  }
}

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

#define N 400
#define W 400

#define max(a,b) (a>b?a:b)

int main() {
  int n, m, s[N], l[N], p[N];
  int dp[N+1][W+1], w[N];
  bool f = false;
  cin >> n;
  for(int i=0; i<n; ++i) cin >> s[i] >> l[i] >> p[i];
  for(int i=0; i<=n; ++i) dp[i][0] = 0;
  for(int i=0; i<=W; ++i) dp[0][i] = 0;
  for(int i=0; i<n; ++i) {
    for(int j=1; j<=W; ++j) {
      dp[i+1][j] = dp[i][j];
      for(int t=s[i]; t<=l[i]; ++t) {
        if(j-t<0) break;
        dp[i+1][j] = max(dp[i+1][j], dp[i][j-t] + p[i]);
      }
    }
  }
  cin >> m;
  for(int i=0; i<m; ++i) {
    cin >> w[i];
    if(dp[n][w[i]]==0) f = true;
  }
  if(f) cout << "-1" << endl;
  else for(int i=0; i<m; ++i) {
      cout << dp[n][w[i]] << endl;
    }
  return 0;
}