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>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main(){
  int N;
  while(cin >> N && N){
    int s[400], l[400], p[400];
    REP(i, N) cin >> s[i] >> l[i] >> p[i];
    const int W = 400;
    vector<int> dp(W + 400, -1);
    dp[0] = 0;
    REP(i, W){
      REP(j, N){
        for(int k = s[j]; k <= l[j]; k++){
          dp[i + k] = max(dp[i + k], dp[i] + p[j]);
        }
      }
    }
    int M; cin >> M;
    vector<int> ans;
    bool ok = true;
    REP(iter, M){
      int w; cin >> w;
      if(dp[w] >= 0) ans.push_back(dp[w]);
      else ok = false;
    }
    if(ok){
      REP(i, ans.size()) cout<<ans[i]<<endl;
    }else{
      cout<<-1<<endl;
    }
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#define N 394
using namespace std;

int main(){
  int n,s[N],l[N],p[N],dp[N][N],m,w,ans[N],flag=0;
  bool used[N];
  cin>>n;
  for(int i=0;i<n;i++) cin>>s[i]>>l[i]>>p[i];
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++) dp[i][j]=-1;
  dp[0][0]=0;
  for(int i=0;i<N;i++)
    for(int j=s[0];j<=l[0];j++){
      if(i-j>=0&&dp[0][i-j]!=-1) dp[0][i]=dp[0][i-j]+p[0];
    }
  for(int i=1;i<n;i++){
    for(int j=0;j<N;j++) used[j]=false;
    for(int j=0;j<N;j++)
      for(int k=s[i];k<=l[i];k++){
	if(j-k>=0&&!used[j]&&dp[i][j-k]!=-1) used[j]=true,dp[i][j]=max(dp[i-1][j],dp[i][j-k]+p[i]);
	else dp[i][j]=max(dp[i][j],dp[i-1][j]);
      }
  }
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    if(dp[n-1][w]==-1){
      flag=1;
      break;
    }else ans[i]=dp[n-1][w];
  }
  if(!flag)
    for(int i=0;i<m;i++) cout<<ans[i]<<endl;
  else cout<<-1<<endl;
  return 0;
}

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

using ll = long long;
using Field = vector<vector<ll>>;
using Graph = vector<vector<ll>>;
using VI = vector<int>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VC = vector<char>;
using PI = pair<int, int>;

#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL<<60;
const int mod = 1000000007;

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 n;
vector<int> s, l, p;
int m;
vector<int> w;

vector<int> dp(400, 0);

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    cin >> n;
    s.resize(n + 1);
    l.resize(n + 1);
    p.resize(n + 1);
    REP(i, n) {
        cin >> s[i] >> l[i] >> p[i];
    }
    cin >> m;
    w.resize(m + 1);
    REP(i, m) {
        cin >> w[i];
    }

    dp[0] = 0;

    for (int i = 1; i < 400; i++) {
        for (int j = 0; j < n; j++) {
            chmax(dp[i], dp[i - 1]);
            if (i >= s[j]) {
                chmax(dp[i], dp[i - s[j]] + p[j]);
            }
        }
    }

    bool err = false;

    vector<int> ans(m + 1, 0);

    for (int i = 0; i < m; i++) {
        if (dp[w[i]] == 0) {
            err = true;
            break;
        } else {
            ans[i] = dp[w[i]];
        }
    }

    if (err) {
        cout << -1 << endl; return 0;
    }

    for(int i = 0; i < m; i++) {
        cout << ans[i] << endl;
    }





    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[999],a[999],l[999],r[999],m[999],N,Q,i,j,k,OK;
main(){
	cin>>N;
	for(i=0;i<N;i++)cin>>l[i]>>r[i]>>m[i];
	for(i=0;i<N;i++)
		for(j=0;j<400;j++)
			for(k=l[i];k<=r[i];k++)
				dp[j+k]=max(dp[j+k],dp[j]+m[i]);
	cin>>Q;
	for(i=0;i<Q;i++){
		cin>>a[i]; 
		if(!dp[a[i]])OK=1;
	}
	if(OK)cout<<"-1"<<endl;
	else for(i=0;i<Q;i++)cout<<dp[a[i]]<<endl;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>

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

	int m;
	cin >> m;

	vector< int > w;

	for(int i = 0; i < m; i++) {
		int a;
		cin >> a;

		w.push_back(a);
	}

	vector< int > dp(394, 0);

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

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

	bool flag = true;
	for(int i = 0; i < m; i++) {
		if(dp[w[i]] == 0) {
			flag = false;
			break;
		}
	}

	if(!flag) {
		cout << "-1" << endl;
	} else {
		for(int i = 0; i < m; i++) {
			cout << dp[w[i]] << endl;
		}
	}
}