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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int main(){
    int N;
    cin >> N;
    int l[N], r[N], p[N];
    rep(i, N) cin >> l[i] >> r[i] >> p[i];
    int dp[401][401];
    rep(i, 401) rep(j, 401) dp[i][j] = -inf;
    dp[0][0] = 0;
    rep(i, 401){
        rep(j, 401){
            rep(k, N){
                if(dp[i][j] == -inf) continue;
                int ni = min(i+l[k], 400), nj = min(j+r[k], 400);
                chmax(dp[ni][nj], dp[i][j]+p[k]);
            }
        }
    }
    int M;
    cin >> M;
    int ans[M];
    rep(i, M){
        ans[i] = -1;
        int w; cin >> w;
        rep2(j, 0, w){
            rep2(k, w, 400){
                chmax(ans[i], dp[j][k]);
            }
        }
        if(ans[i] == -1) {cout << -1 << endl; return 0;}
    }
    rep(i, M) cout << ans[i] << endl;
}

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

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT_ON(bit, i) (bit & (1LL << i))

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template<typename T> using VVVV = std::vector<std::vector<std::vector<std::vector<T>>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }
template<class T> inline std::vector<T> unique(std::vector<T> v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    return v;
}

const int INF = 1 << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

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

    int N;
    cin >> N;
    V<int> points(400);
    FOR(i, 0, N) {
        int s, l, p;
        cin >> s >> l >> p;
        FOR(j, s, l + 1) {
            points[j] = max(points[j], p);
        }
    }

    V<LL> dp(401, -1);
    dp[0] = 0;
    FOR(i, 0, points.size()) {
        FOR(j, 0, dp.size()) {
            if (dp[j] == -1) {
                continue;
            }
            if (j + i < dp.size()) {
                dp[j + i] = max(dp[j + i], dp[j] + points[i]);
            }
        }
    }

    int M;
    cin >> M;
    V<LL> ans(M);
    FOR(i, 0, M) {
        int w;
        cin >> w;
        ans[i] = dp[w];
        if (ans[i] == 0) {
            print(-1);
            return 0;
        }
    }

    FOR(i, 0, M) {
        print(ans[i]);
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;

main(){
	int n,m,i,j;
	cin >> n;
	vector<int> dp(400, -1);
	dp[0] = 0;
	while(n--){
		int s,d,p;
		cin >>s>>d>>p;
		for(i=0;i<394;i++){
			if(dp[i]==-1) continue;
			for(j=s;j<=d&&i+j<=393;j++) dp[i+j] = max(dp[i+j], dp[i] + p);
		}
	}
	cin >> n;
	vector<int> ans;
	while(n--){
		cin >> m;
		if(dp[m] == -1){
			cout << "-1" << endl;
			return 0;
		}
		ans.push_back(dp[m]);
	}
	for(i=0;i<ans.size();i++) cout << ans[i] << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int N, M, mx = 0;
vector<int> under(400, 0);
vector<int> upper(400, 0);
vector<int> point(400, 0);
vector<int> span(400, 0);
vector<int> dp(400, -1);

int main(void){
	
	cin >> N;
	for(int i = 0; i < N; ++i) cin >> under[i] >> upper[i] >> point[i];
	cin >> M;
	for(int i = 0; i < M; ++i){
	    cin >> span[i]; mx = max(mx, span[i]);
	}
	
	dp[0] = 0;
    for(int s = 0; s <= mx; ++s){
        if(~dp[s]){
            for(int i = 0; i < N; ++i){
                for(int j = under[i]; j <= upper[i]; ++j){
                    if(s + j <= mx) dp[s + j] = max(dp[s + j], dp[s] + point[i]);
                }
            }
        }
        else{
            for(int i = 0; i < M; ++i){
                if(s == span[i]){
                    cout << -1 << endl;
                    return 0;
                }
            }
        }
    }
	for(int i = 0; i < M; ++i) cout << dp[span[i]] << endl;
	
	return 0;
}