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;
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;
    vector<int> ans(m);
    bool ng = false;
    for (int i = 0; i < m; i++) {
        int w;
        cin >> w;
        ans[i] = score[w];
        ng |= score[w]<0;
    }
    if (ng) cout << -1 << endl;
    else {
        for (int i = 0; i < m; i++) cout << ans[i] << endl;
    }

    return 0;
}

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

void chmax( int &a, int b){ if( a < b){ swap(a,b);} return;}

int main(){
  int N; cin >> N;
  int dp[100100]; // dp[i]で、長さがちょうどiのメロディの最高得点を表す -1は未更新
  vector<int> s(N); vector<int> l(N); vector<int> p(N);
  vector<int> w(N);
  for(int i = 0; i < 100100; i++){ dp[i] = -1;}
  
  dp[0] = 0;
  
  for(int i = 0; i < N; i++){ 
    cin >> s[i] >> l[i] >> p[i];}
  
  for(int i = 0; i < 10010; i++){
    for(int j = 0; j < N; j++){
      if( dp[i] == -1){ continue;} 
      for(int k = s[j]; k <= l[j]; k++){
        if( k+i < 100100){
        chmax( dp[i+k] , dp[i]+p[j]);}
      }
    }
  }

    
  
  int M; cin >> M;
  vector<int> ans;
  for(int j = 0; j < M; j++){ 
    int w; cin >> w;
    int a = dp[w];
    if( a == -1){ cout << -1 << endl; return 0;}
    ans.push_back(a);
  }
  
  for(auto ne: ans){ cout << ne << endl;}
  
  return 0;}

C++: #include<iostream>
#include<set>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class Phrase {
    public:
    int s,l,p;
    Phrase(int s,int l,int p) {

        this->s = s;
        this->l = l;
        this->p = p;
    }
    Phrase() {
        s=l=p=0;
    }

};

int n;
Phrase phra[400];
int m;
int w[400];
int dp[400];

int main() {

    cin>>n;
    rep(i,n) {
        int s,l,p;
        cin>>s>>l>>p;
        phra[i] = Phrase(s,l,p);
    }
    cin>>m;
    rep(i,m) cin>>w[i];

    rep(i,n) {
        for(int j=phra[i].s;j<=phra[i].l;j++) {
            for(int k=0;k<400;k+=j) {
                dp[k+j] = max(dp[k+j],dp[k]+phra[i].p);
            }
        }
    }

    multiset<int> ans;
    
    rep(i,m) ans.insert(dp[w[i]]);    
    if(ans.find(-1) == ans.end()) {
        cout<<"-1"<<endl;
        return 0;
    }

    multiset<int>::iterator it = ans.begin();
    while(it != ans.end()) {
        cout<<(*it)<<endl;
        it++;
    }




    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

const ll mod = 1e9+7;

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

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int dp[400][400];

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;
  vector<int> w(m);
  rep(i, m) cin >> w[i];
  for(int i=1; i<=n; i++) {
    rep(j, 395) {
      chmax(dp[i][j], dp[i-1][j]);
      for(int k=s[i-1]; k<=l[i-1]; k++) {
        if(j-k >= 0)
          chmax(dp[i][j], dp[i][j-k]+p[i-1]);
        else
          break;
      }
    }
  }
  rep(i, m) {
    if(!dp[n][w[i]]) {
      cout << -1 << endl;
      return 0;
    }
  }
  rep(i, m)
    cout << dp[n][w[i]] << endl;
  return 0;
}