Go: ///
// File:  2502.go
// Author: ymiyamoto
//
// Created on Sat Mar 24 00:44:45 2018
//
package main

import (
	"fmt"
)

type phrase struct {
	s, l, p int
}

const melody_len int = 393

var n, m 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, melody_len+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>
#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;
  }
}

C++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n), b(n), p(n);
    for(int i=0; i<n; ++i)
        cin >> a[i] >> b[i] >> p[i];

    vector<int> dp(393+1, -1);
    dp[0] = 0;
    for(int i=0; i<393; ++i){
        if(dp[i] == -1)
            continue;
        for(int j=0; j<n; ++j){
            for(int k=a[j]; k<=b[j]; ++k){
                int t = i + k;
                if(t <= 393)
                    dp[t] = max(dp[t], dp[i] + p[j]);
            }
        }
    }

    int m;
    cin >> m;
    vector<int> ret(m);
    for(int i=0; i<m; ++i){
        int w;
        cin >> w;
        if(dp[w] == -1){
            cout << -1 << endl;
            return 0;
        }
        ret[i] = dp[w];
    }

    for(int i=0; i<m; ++i)
        cout << ret[i] << endl;

    return 0;
}

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

int dp[400];
int n,s,l,p,w,ans[400];

int main(){
  //  pair<int ,int> P;


  vector<pair<int,int> > v;

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

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

  for(int i = 1; i < 400;i++){
    int max = -1;
    for(int j = 0; j <= (i)/2; j++){
      if(dp[j] != -1 && dp[i-j] != -1){
	if(max < dp[j] + dp[i-j]) max =  dp[j] + dp[i-j];
      } 
    }
    dp[i] = max;
  }

  int m;
  cin >> m;
  vector<int> ans;
  int f = 0;
  for(int i = 0; i < m;i++){
    cin >> w;
    if(dp[w] == -1){
      cout << "-1\n";
      f = 1;
    }
    else{
      ans.push_back(dp[w]); 
    }
  }

  if(f != 1){
    for(int i = 0; i < ans.size() ;i++){
      cout << ans[i] << endl;
    }
  }
  return 0;
}

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

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