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 <iostream>
using namespace std;

int main(void)
{
	int n, s[393], l[393],p[393];
	cin >> n;
	for(int i = 0; i < n; i++) cin >> s[i] >> l[i] >> p[i];
	int m, w[393];
	cin >> m;
	int longest = 0;
	for(int i = 0; i < m; i++){
		cin >> w[i];
		longest = max(longest,w[i]);
	}
	
	int point[394][394] = {0};
	for(int j = 0; j < n; j++){//j番目のフレーズ
		for(int k = 0; k < longest; k++){
			if(k == 0 || point[j][k]){
				point[j + 1][k] = point[j][k];
				for(int x = s[j]; x <= l[j]; x++){
					if(k + x <= longest){
						point[j + 1][k + x] = max(point[j + 1][k + x], (point[j][k] + p[j]));
					}
				}
			}
		}
	}

	int max[393];
	for(int i = 0; i < m; i++){//i番目のメロディー
		if(point[n][w[i]]) max[i] = point[n][w[i]];
		else {max[0] = -1; break;}
	}


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

}

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
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;}

// 0 false, 1 true 
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

int main() {
    std::cout << std::fixed << std::setprecision(15);
    ll n; cin >> n;
    //vector<pair<ll,ll>> vec; 
    vector<ll> vec(394); 
    rep(i, n){
        ll s, l, p; cin >> s >> l >> p;
        for(ll j = s; j <= l; j++){
            //vec.push_back({j, p});
            vec[j] = max(vec[j], p);
        }
    }
    vector<pair<ll,ll>> vec2; 
    
    rep(i, 394){
        if(vec[i] > 0){
            vec2.push_back(make_pair(i, vec[i]));
        }
    }
    /*
    rep(i, vec2.size()){
        cout << vec2[i].first << " " << vec2[i].second << endl;
    }
    */
    

    vector<vector<ll>> dp(vec2.size()+1, vector<ll>(394, -1));
    dp[0][0] = 0;
    dp[1][0] = 0;

    for(ll i = 0; i < vec2.size(); i++){
        for(ll j = 0; j <= 393; j++){
            if(j - vec2[i].first >= 0 && dp[i + 1][j - vec2[i].first] != -1){
            //if(j - i >= 0 && dp[i + 1][j - i] != -1){
                dp[i+1][j] = max(dp[i][j], dp[i + 1][j - vec2[i].first] + vec2[i].second);
                //dp[i+1][j] = max(dp[i][j], dp[i + 1][j - i] + vec[i]);
            }else{
                dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            }
        }
    }

    ll m; cin >> m;
    vector<ll> ans;
    rep(i, m){
        ll w; cin >> w;
        ans.push_back(dp[vec2.size()][w]);
        
        if(dp[vec2.size()][w] == -1){
            cout << -1 << endl;
            return 0;
        }
        
    }
    
    rep(i, m){
        cout << ans[i] << endl;
    }
    

}

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;

int dp[400][400];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  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];
  }
  
  int m;
  cin >> m;

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

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

  vector<int> ans;
  for (int i = 0; i < m; i++) {
    if (dp[n][w[i]] < 0) {
      cout << -1 << endl;
      return 0;
    }
    ans.emplace_back(dp[n][w[i]]);
  }
  for (auto &i : ans) {
    cout << i << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

int main(){
	int n,m;
	cin >> n;
	int s,l,p;
	int w[400]={0,};
	for(int i=0;i<n;i++){
		cin >> s >> l >> p;
		for(int j=s;j<=l;j++){
			for(int k=0;k+j<=399;k++){
				w[k+j]=max(w[k+j],w[k]+p);
			}
		}
	}
	cin >> m;
	for(int i=0;i<m;i++){
		cin >> s;
		cout << w[s] << endl;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
struct tri {
    int first,second,third; 
    tri(int first = 0,int second = 0,int third = 0):first(first),second(second),third(third) {}
};

int main() {
    int N; cin >> N;
    vector<tri> phrase(N);
    rep(i,N) cin >> phrase[i].first >> phrase[i].second >> phrase[i].third;
    int M; cin >> M;
    vector<int> W(M);
    rep(i,M) cin >> W[i];

    vector<int> dp(400,0);
    rep(i,N) {
        int l = phrase[i].first,r = phrase[i].second,p = phrase[i].third;
        rep(j,400) {
            for (int k = max(j - r,0); k < min(j - l + 1,400);++k) {
                dp[j] = max(dp[j],dp[k] + p);
            }
        }
    }
    vector<int> ans(M);
    bool flag = true;
    rep(i,M) {
        if (dp[W[i]] > 0) ans[i] = dp[W[i]];
        else {flag = false; break;}
    }
    if (flag) {
        rep(i,M) cout << ans[i] << '\n'; 
    }
    else cout << -1 << '\n';
    return 0;
}