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 int long long

#define ll long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define P pair<ll,ll>
int MOD = 1000000007;
const ll INF = 1LL << 60;

int n;
int s[400], l[400], p[400];
int m;
int dp[400];

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	rep(i, n) {
		cin >> s[i] >> l[i] >> p[i];
		rep(j, 400) {
			for (int k = s[i]; k <= l[i]; k++) {
				if(j - k >= 0)dp[j] = max(dp[j], dp[j - k] + p[i]);
			}
		}
	}
	cin >> m;
	vector<int>ans;
	rep(i, m) {
		int w; cin >> w;
		if (dp[w] == 0) {
			cout << -1 << endl; return 0;
		}
		ans.push_back(dp[w]);
	}
	rep(i, m) {
		cout << ans[i] << endl;
	}
	return 0;
}

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)
#define minit(a,b) memset(a,b,sizeof(a))

void maxUpdate(int& a, int b) {
	a = max(a, b);
}

int n, s[555], l[555], p[555], dp[555][555], m, dp2[555];

int main() {
	cin >> n;
	for_(i,0,n) cin >> s[i] >> l[i] >> p[i];
	
	minit(dp, -1);
	dp[0][0] = 0;

	for_(i,0,n) for_(j,0,395) {
		if (dp[i][j] == -1) continue;
		maxUpdate(dp[i+1][j], dp[i][j]);
		for_(k,s[i],l[i]+1) if (j + k <= 393) maxUpdate(dp[i+1][j+k], dp[i][j] + p[i]);
	}
	
	vector< int > ans;
	bool flag = true;
	
	cin >> m;
	for_(i,0,m) {
		int w;
		cin >> w;
		ans.push_back(dp[n][w]);
		if (ans[i] <= 0) flag = false;
	}
	
	if (flag) {
		for_(i,0,m) cout << ans[i] << endl;
	} else {
		cout << -1 << endl;
	}
}

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

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

	int m;
	cin >> m;

	vector< int > w;

	for(int i = 0; i < m; i++) {
		int a;
		cin >> a;

		w.push_back(a);
	}

	vector< int > dp(394, 0);

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

	for(int i = 0; i < n; i++) {
		for(int j = 1; j <= 393; j++) {
			if(j + s[i] < 393) {
				dp[j + s[i]] = max(dp[j + s[i]], dp[j] + p[i]);
			}
			if(j + l[i] < 393) {
				dp[j + l[i]] = max(dp[j + l[i]], dp[j] + p[i]);
			}
		}
	}

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

	if(!flag) {

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

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(b);i>(a);--i)
#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)
#define pb push_back
#define mp make_pair
#define INF (1<<30)-1
#define MOD 1000000007
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
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 (b<a) { a = b; return 1; } return 0; }
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a, b)*b;}

int n, m, s[400], l[400], p[400], w[400], dp[400][400];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
    cin >> n;
    rep(i, 0, n) cin >> s[i] >> l[i] >> p[i];
    cin >> m;
    rep(i, 0, m) cin >> w[i];
    vector<int> ans;
    rep(f, 0, m) {
        rep(i, 0, n) erep(j, 0, w[f]) {
            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]);
        }
        if (dp[n][w[f]] == 0) {
            cout << -1 << endl;
            return 0;
        } else {
            ans.pb(dp[n][w[f]]);
            memset(dp, 0, sizeof(dp));
        }
    }
    rep(i, 0, ans.size()) cout << ans[i] << endl;
    return 0;
}

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

int main() {
    int N;
    cin >> N;
    vector<int> s(N), l(N), p(N);
    for (int i = 0; i < N; i++) {
        cin >> s[i] >> l[i] >> p[i];
    }

    vector<int> dp(394, -1);
    dp[0] = 0;

    int len = dp.size();
    for (int i = 0; i < 394; i++) {
        if (dp[i] < 0) continue;
        for (int j = 0; j < N; j++) {
            for (int k = s[j]; k <= l[j]; k++) {
                if (i + k < len) dp[i + k] = max(dp[i + k], dp[i] + p[j]);
            }
        }
    }

    int M;
    cin >> M;

    vector<int> m(M);
    bool flag = true;
    for (int i = 0; i < M; i++) {
        cin >> m[i];
        if (dp[m[i]] < 0) flag = false;
    }
    if (flag) {
        for (auto i : m) cout << dp[i] << endl;
    }
    else {
        cout << -1 << endl;
    }

    return 0;
}

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

#define MAX (393+1)

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

	cin>>n;

	for(int i=0;i<MAX;i++){
		dp[i]=-1;
	}

	dp[0]=0;

	for(int i=0;i<n;i++){
		cin>>s>>l>>p;
		f=true;
		while(f){
			f=false;
			for(int j=s;j<=l;j++){
				for(int k=0;k<MAX-j;k++){
					if(dp[k]>=0){
						if(dp[k+j]<dp[k]+p){
							dp[k+j]=dp[k]+p;
							f=true;
						}
					}
				}
			}
		}
	}

	cin>>m;
	for(int i=0;i<m;i++){
		cin>>w;
		cout<<dp[w]<<endl;
	}

	return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

const int LEN = 393;

int lower[LEN], upper[LEN], point[LEN];
int dp[LEN + 1][LEN + 1];
int melody[LEN];

int main()
{
    int n;
    cin >> n;
    rep(i, n)
    {
        cin >> lower[i] >> upper[i] >> point[i];
    }

    int m;
    cin >> m;
    rep(i, m)
    {
        cin >> melody[i];
    }

    irep(i, n) irep(j, LEN)
    {
        if (i == 0) {
            dp[i][j] = j == 0 ? 0 : -1;
        } else {
            dp[i][j] = dp[i - 1][j];
            for (int k = lower[i - 1]; k <= upper[i - 1]; k++) {
                if (j - k >= 0 && dp[i][j - k] >= 0) {
                    dp[i][j] = max(dp[i][j], dp[i][j - k] + point[i - 1]);
                }
            }
        }
    }

    rep(i, m)
    {
        if (dp[n][melody[i]] < 0) {
            cout << -1 << endl;
            return 0;
        }
    }

    rep(i, m)
    {
        cout << dp[n][melody[i]] << endl;
    }

    return 0;
}