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] = -1;
    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 = s; j <= l; 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] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef long long int ll;

int n;

int dpdiv[400]={0};
int dppnt[400]={0};

int set(int s, int d, int p){
	if(dppnt[s] < p){
		if(dpdiv[s] > d) set(s + d, dpdiv[s] - d, dppnt[s]);
		dppnt[s] = p;
		dpdiv[s] = d;
	}else{
		if(dpdiv[s] < d) set(s + dpdiv[s], d - dpdiv[s], p);
	}
}

main(){
	int i,j;
	cin >> n;
	while(n--){
		int s,d,p;
		cin >>s>>d>>p;
		for(i=393;i;i--) if(dppnt[i]) set(i+s, dpdiv[i] + d-s, p+dppnt[i]);
		set(s, d-s, p);
	}
	for(i=0;i<394;i++){
		for(j=1;j<=dpdiv[i];j++){
			set(i+j, dpdiv[i]-j, dppnt[i]);
		}
	}
	cin >> n;
	vector<int> ans;
	while(n--){
		int m;
		cin >> m;
		if(!dppnt[m]){
			cout << "-1" << endl;
			return 0;
		}
		ans.push_back(dppnt[m]);
	}
	for(i=0;i<ans.size();i++) cout << ans[i] << endl;
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main(){
	int n;
	cin>>n;
	vi s(n),l(n),p(n);
	REP(i,n){
		cin>>s[i]>>l[i]>>p[i];
	}
	int m;
	cin>>m;
	vi w(m);
	REP(i,m){
		cin>>w[i];
	}
	vvi dp(n+1,vi(400+1));
	REP(i,n){
		vi available(400+1);
		for(int rep=1;s[i]*rep<=400;rep++){
			FOR(k,s[i]*rep,min(l[i]*rep,400)+1){
				available[k]=max(available[k],rep);
			}
		}
		REP(k,400+1){
			if(available[k]){
				REP(j,400+1){
					if(j-k>=0){
						dp[i+1][j]=max(dp[i+1][j],dp[i][j-k]+p[i]*available[k]);
					}
					dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
				}
			}
		}
	}
	vi ans(m);
	bool flag=false;
	REP(melody,m){
		ans[melody]=dp[n][w[melody]];
		if(ans[melody]==0){
			flag=true;
		}
	}
	if(flag){
		cout<<-1<<endl;
	}else{
		REP(i,m){
			cout<<ans[i]<<endl;
		}
	}

}

C++: #include<iostream>
#include<vector>
#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=-1;
    }

};

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,400) dp[i]=-1;
    dp[0]=0;

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

    vector<int> ans;
    bool flag = false;
    
    rep(i,m) {
        int t = dp[w[i]];
        if(t < 0) flag=true;
        else ans.push_back(t);
    }
    if(flag) {
        cout<<"-1"<<endl;
        return 0;
    }

    rep(i,ans.size()) cout<<ans[i]<<endl;

    return 0;
}