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 <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
template<class T> inline bool amax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool amin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> ostream& operator << (ostream &os, const vector<T> &v) { os << "["; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) { os << (it != v.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T> &s) { os << "["; for (typename set<T>::const_iterator it = s.begin(); it != s.end(); it++) { os << (it != s.begin() ? ", " : "") << *it; } os << "]"; return os; }
template<class Key, class Val> ostream& operator << (ostream &os, const map<Key, Val> &m) { os << "{"; for (typename map<Key, Val>::const_iterator it = m.begin(); it != m.end(); it++) { os << (it != m.begin() ? ", " : "") << it->first << ":" << it->second; } os << "}"; return os; }
template<class T, class S> ostream& operator << (ostream &os, const pair<T, S> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <class T> istream& operator >> (istream &is, vector<T> &v) { for (size_t i = 0; i < v.size(); i++) is >> v[i]; return is; }
template <class Target, class Source> inline Target lexical_cast (const Source &s) { Target t; stringstream ss; ss << s; ss >> t; return t; }

//> v < ^ (clock wise)
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
const int INFI = 1<<28;
const long long int INFL = 1LL<<60;
const double INFD = 1e+300;
const float INFF = 1e+100;
const double EPS = 1e-8;
const long long int MOD = 1000000007;

int solve ();
int main () {
    cout.setf(ios::fixed); cout.precision(10);
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}

int solve () {
    int n;
    cin >> n;
    int s, l, p;
    int score[160000];
    for (int i = 0; i < 160000; i++) score[i] = -INFI;
    score[0] = 0;
    for (int i = 0; i < n; i++) {
        cin >> s >> l >> p;
        for (int i = 160000-1; i >= 0; i--) {
            for (int j = l; j >= s; j--) {
                if (i+j < 160000) score[i+j] = max(score[i+j], score[i]+p);
            }
        }
    }

    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        int w;
        cin >> w;
        cout << (score[w]<0?-1:score[w]) << endl;
    }

    return 0;
}

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

using namespace std;

int main(){
  int n;
  cin>> n;
  int d,u,s,dp[400];
  memset(dp,-1,sizeof(dp));
  dp[0]=0;
  for(int i=0;i<n;i++){
    cin>>d>>u>>s;
    for(int j=0;j<400;j++)
      for(int k=d;k<=u;k++)
	if(j+k<400 && dp[j]!=-1) dp[j+k] = max(dp[j+k],dp[j]+s);
  }
  
  int q,flg=0,m[400];
  cin>> q;
  for(int i=0;i<q;i++){
    cin >> m[i];
    if(dp[m[i]] == -1) flg= 1;
  }

  if(flg) cout << -1<<endl;
  else 
    for(int i=0;i<q;i++) cout << dp[m[i]]<<endl;
  return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

int main(){
  int pattern_num;


  while(~scanf("%d",&pattern_num)){

    int len2point[400];
    memset(len2point,-1,sizeof(len2point));

    for(int pattern_idx=0;pattern_idx<pattern_num;pattern_idx++){
      int lower_len,upper_len,point;
      scanf("%d %d %d",&lower_len,&upper_len,&point);
      for(int len=lower_len;len<=upper_len;len++){
	len2point[len] = max(len2point[len],point);
      }
    }
      
    int m;
    scanf("%d",&m);
    vector<int> point_vec;
    for(int melody_idx=0;melody_idx<m;melody_idx++){
      int w;
      scanf("%d",&w);
      int dp[400];
      memset(dp,-1,sizeof(dp));
      dp[0] = 0;
      for(int total_len=0;total_len<=w;total_len++){
	for(int add_bar_len=0;add_bar_len<=total_len;add_bar_len++){
	  if(len2point[add_bar_len] == -1) continue;
	  dp[total_len] = max(len2point[add_bar_len]+dp[total_len-add_bar_len],dp[total_len]);
	}
      }
      point_vec.push_back(dp[w]);
    }

    bool isok = true;
    for(int i=0;i<point_vec.size();i++){
      if(point_vec[i] == -1){
	isok = false;
      }
    }

    if(isok){
      for(int i=0;i<point_vec.size();i++){
	printf("%d\n",point_vec[i]);
      }
    }

    else{
      printf("%d\n",-1);
    }
  }
}

C++: // 5/6
// 個数制限なしナップサック問題

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

ll dp[400][400];
// const int INF = 1000000000;

int main() {
    int n;  // フレーズの数 < 393
    cin >> n;

    vector<int> s(n), l(n), p(n);  // 各フレーズの下限、上限、得点  < 393
    rep(i, n) cin >> s[i] >> l[i] >> p[i];

    int m;  // メロディの数  < 393
    cin >> m;

    vector<int> w(m);  // メロディの長さ  < 393 
    rep(i, m) cin >> w[i];

    int W = 393;  // メロディの長さの最大値

    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;

    rep(i, n) {
        rep(j, W + 1) {
            if (s[i] > j) {
                dp[i + 1][j] = dp[i][j];
            }
            else {
                // i番目を使わない場合で初期化
                dp[i + 1][j] = dp[i][j];

                // i番目を新しく使うときは、どの長さ（k）で使うかを分岐
                for (int k = s[i]; k <= min(l[i], j); k++) {
                    dp[i + 1][j] = max(dp[i + 1][j - k] + p[i], dp[i + 1][j]);  // マイナス１ならそれはそれで。
                }
            }
        }
    }

    bool b = true;
    
    rep(i, m) {
        if (dp[n][w[i]] == -1) b = false;
    }

    if (b) {
        rep(i, m) {
            cout << dp[n][w[i]] << endl;
        }
    } else {
        cout << -1 << endl;
    }

}

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

int main()
{
  int s[400];
  int l[400];
  int p[400];
  int w[400];

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

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

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

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


}