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 <cstring>

using namespace std;

int main(){
  int n;
  cin>> n;
  int d[400],u[400],s[400];
  for(int i=0;i<n;i++) cin>>d[i]>>u[i]>>s[i];
  
  int dp[400]={};
  memset(dp,-1,sizeof(dp));
  dp[0]=0;

  for(int i=0;i<n;i++)
    for(int j=0;j<400;j++)
      for(int k=d[i];k<=u[i];k++)
	if(j+k<400 && dp[j]!=-1) dp[j+k] = max(dp[j+k],dp[j]+s[i]);
  
  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++: #include<bits/stdc++.h>
using namespace std;

int main(){

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

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

    vector<int> v(400, 0);

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

    int dp[410][410] = {};
    
    dp[0][0] = 0;

    for(int i = 0; i < 400; i++){
        for(int j = 0; j < 400; j++){
            if(j < i){
                dp[i + 1][j] = dp[i][j];
            }else{
                dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - i] + v[i]);
            }
        }
    }
    vector<int> ans;
    for(int i = 0; i < m; i++){
        if(dp[399][w[i]] == 0){
            cout << -1 << endl;
            return 0;
        }

        ans.push_back(dp[399][w[i]]);
    }

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

    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2502
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


int main(){
    int N;
    cin >> N;
    vector<int> s(N+1), l(N+1), p(N+1);
    rep(i, N) cin >> s.at(i+1) >> l.at(i+1) >> p.at(i+1);
    vector<vector<int> > dp(N+1, vector<int>(395, 0));
    rep(i, N){
        rep(j, 394){
            chmax(dp[i+1][j], dp[i][j]);
            rep2(k, s.at(i+1), l.at(i+1)+1){
                if(j-k>=0){
                    chmax(dp[i+1][j], dp[i+1][j-k]+p.at(i+1));
                }
            }
        }
    }
    int M;
    cin >> M;
    vector<int> answer(M);
    rep(i, M){
        int w;
        cin >> w;
        answer.at(i) = dp[N][w];
        if(dp[N][w] == 0){
            cout << -1 <<endl;
            return 0;
        }
    }
    rep(i, M){
        cout << answer.at(i) << endl;
    }

    
}

C++: #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <tuple>
#include <utility>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<int,int,int> T3;

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

	int dp[400] = {};

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

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

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

	if(flag)cout << -1 << endl;
	else{
		for(int a:ans)cout << a << endl;
	}
}

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

int main(){
  int n,s[N],l[N],p[N],m,w,a[N],f=0,dp[N];
  cin>>n;
  for(int i=0;i<n;i++) cin>>s[i]>>l[i]>>p[i];
  for(int i=0;i<N;i++) dp[i]=-1;
  dp[0]=0;
  for(int i=0;i<n;i++)
    for(int j=0;j<N;j++)
      for(int k=s[i];k<=l[i];k++)
	if(j-k>=0&&dp[j-k]!=-1) dp[j]=max(dp[j],dp[j-k]+p[i]);
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    dp[w]==-1?f=1:a[i]=dp[w];
  }
  if(f) cout<<-1<<endl;
  else
    for(int i=0;i<m;i++) cout<<a[i]<<endl;
  return 0;  
}

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

using namespace std;

int MAX = 393;

int main() {
	int n;
	cin >> n;

	vector<int> S(n), L(n), P(n), SUB(n);
	for (int i=0; i<n; i++) {
		cin >> S[i] >> L[i] >> P[i];
		SUB[i] = L[i]-S[i];
	}

	int m;
	cin >> m;

	vector<int> W(m);
	int M = -1;
	for (int i=0; i<m; i++) {
		cin >> W[i];
		M = max(M, W[i]);
	}

	int **dp = new int*[MAX+10];
	for (int i=0; i<MAX+10; i++) {
		dp[i] = new int[3*MAX];
		for (int j=0; j<3*MAX; j++)
			dp[i][j] = 0;
	}

	dp[0][0] = 0;
	for (int i=1; i*S[0]<=M; i++)
		for (int j=i*S[0]; j<=i*S[0]+SUB[0]; j++)
			dp[0][j] = max(dp[0][j], i*P[0]);
	
	for (int i=1; i<n; i++) {
		for (int j=0; j<=M; j++) {
			for (int k=S[i]; k<=S[i]+SUB[i]; k++) {
				dp[i][j+k] = max(max(dp[i][j+k], dp[i-1][j+k]), dp[i-1][j]+P[i]);
			}
		}
	}

	vector<int> point(m);
	bool flag = false;
	for (int i=0; i<m; i++) {
		point[i] = 0;
		for (int j=0; j<n; j++)
			point[i] = max(point[i], dp[j][W[i]]);
		if (!point[i]) {
			flag = true;
			break;
		}
	}
	
	if (flag) cout << -1 << endl;
	else {
		for (int i=0; i<m; i++)
			cout << point[i] << endl;
	}

	for (int i=0; i<MAX+10; i++)
		delete [] dp[i];
	delete [] dp;

	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
static const int MAX = 500;

int n;
int s[MAX], l[MAX], p[MAX];

int solve(int w){
  int dp[MAX];
  for ( int i = 0; i <= w; i++ ) dp[i] = -1;
  dp[0] = 0;
  for ( int i = 0; i <= w; i++ ){
    if ( dp[i] == -1 ) continue;
    for ( int k = 0; k < n; k++ ){
      for ( int j = i+s[k]; j <= i+l[k]; j++ ){
	if ( j > w ) continue;
	dp[j] = max(dp[j], dp[i] + p[k]);
      }
    }
  }
  return dp[w];
}

main(){
  int m, w;
  vector<int> ans;
  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;
    int o = solve(w);
    if ( o == -1 ){
      cout << -1 << endl; return 0;
    }
    ans.push_back(o);
  }
  for ( int i = 0; i < m; i++ ) cout << ans[i] << endl;
  
}