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 <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define X first
#define Y second
using namespace std;
const int MAX = 394; 
int dp[394];

void DP(int amount, int v){
	for(int i = 1; i < MAX; i++){
		if(amount <= i && dp[i-amount]) dp[i] = dp[i-amount]+v>dp[i] ? dp[i-amount]+v : dp[i];
	}
}

int main(){
	int n;
	cin >> n;
	fill(dp, dp+MAX, 0);
	while(n--){
		int up, down, v;
		cin >> down >> up >> v;
		while(down <= MAX){
			if(up>=MAX) up = MAX-1;
			for(int i = down; i <= up; i++) dp[i] = dp[i]<v?v:dp[i];
			for(int i = 0; i < 2; i++){
				if(i) DP(up, v);
				else DP(down, v);
			}
			up += up;
			down += down;
			v += v;
		}
	}
	int m;
	cin >> m;
	vector<int> ans;
	while(m--){
		int input;
		cin >> input;
		if(dp[input]) ans.push_back(dp[input]);
		else{
			cout << "-1" << endl;
			return 0;
		}
	}
	for(int i = 0; i < ans.size(); i++){
		cout << ans[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
using ll = long long;
using namespace std;

int w[401];
vector<tuple<int, int, int>> v;
int dp[401]; //indexの番号 重さ 最大コストをもつ
int const MOD = 1e9 + 7;
int main(void)
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        int r, l, p;
        cin >> r >> l >> p;
        v.push_back(make_tuple(r, l, p));
    }

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

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= 393; ++j)
        {
            for (int k = get<0>(v[i]); k <= get<1>(v[i]); ++k)
            {
                if (j - k >= 0)
                {
                    dp[j] = max(dp[j], dp[j - k] + get<2>(v[i]));
                }
                else
                {
                    dp[j] = dp[j];
                }
            }
        }
    }

    bool ok = true;
    vector<int> ans;
    for (int i = 0; i < m; ++i)
    {
        if (dp[w[i]] == 0)
            ok = false;
        ans.push_back(dp[w[i]]);
    }
    if (!ok)
        cout << -1 << endl;
    else
    {
        for (auto e : ans)
            cout << e << 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(0) != 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>
using namespace std;

int main(){
  int N,W;
  cin>>N;

  vector<tuple<int,int,int>> a(N);

  int i,j;
  for(i=0;i<N;i++){
    int s,l,p;
    cin>>s>>l>>p;
    get<0>(a.at(i))=s;
    get<1>(a.at(i))=l;
    get<2>(a.at(i))=p;
  }

  int k,m;
  bool chek=0;
  cin>>m;
  vector<int> ans(m);
  for(k=0;k<m;k++){
    cin>>W;
    vector<vector<long long>> dp(N+1,vector<long long>(W+1,0));
    for(i=0;i<N;i++){
      for(j=0;j<=W;j++){
        if(j-get<0>(a.at(i))<0){
          dp.at(i+1).at(j)=dp.at(i).at(j);
        }else{
          dp.at(i+1).at(j)=max(dp.at(i).at(j),dp.at(i+1).at(j-get<0>(a.at(i)))+get<2>(a.at(i)));
        }
      }
    }
    if(dp.at(N).at(W)==0){
      chek=1;
      break;
    }
    ans.at(k)=dp.at(N).at(W);
  }
  if(chek)
  cout<<-1<<endl;
  else{
    for(k=0;k<m;k++)
    cout<<ans.at(k)<<endl;
  }

}

C++: #include <cstdio>
#include <algorithm>
#include <map>


using namespace std;
#define rep(i,n) for(i=0;i<n;i++)

typedef pair<int,int> P;

int f[400];
int can[400];
int w[400];

int main(){
	
	int i,j,k;
	int n,m,s,l,p,M;
	bool g;
	
	rep(i,400)f[i]=0;
	
	scanf("%d",&n);
	k=0;
	
	rep(i,n){
		scanf("%d %d %d",&s,&l,&p);
		for(j=s;j<=l;j++){
			f[j]= max(f[j],p);
		}
	}
	
	//rep(i,40){printf(" $%3d:%3d\n",i,f[i]);}
	
	rep(i,400){
		if(f[i]==0)continue;
		for(j=i;j<400;j++){
			if(j-i==0 ||( j-i>0 && can[j-i]!= 0)){
				can[j] = max(can[j],can[j-i]+f[i]);
			}
		}
		//rep(j,40){printf("!%2d  %3d:%3d\n",i,j,can[j]);}
	}
	
	//rep(i,40){printf(" #%3d:%3d\n",i,can[i]);}
	
	
	
	scanf("%d",&m);
	
	rep(i,m){
		scanf("%d",&w[i]);
	}
	
	
	g=false;
	rep(i,m){
		if(can[w[i]]==0){
			g=true;
			break;
		}
	}
	if(g){
		puts("-1");
	}else{
		rep(i,m){
			printf("%d\n",can[w[i]]);
		}	
	}
	
	
}

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

using namespace std;

int main(void){
  int n,m,s[400],l[400],p[400],w[400],dp[400];
  
  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[i];
  
  fill(dp,dp+400,-1);
  dp[0]=0;

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

C++: #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>;

const long long MOD = 1000000007;
const long long INF = 1LL << 60;

template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}

template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}

int main()
{
  int N;
  cin >> N;
  static int dp[400];
  int s, l, p;
  for (int i = 1; i <= N; i++)
  {
    cin >> s >> l >> p;
    for (int j = 1; j <= 400; j++)
    {
      for (int k = s; j - k >= 0 && k <= l; k++)
      {
        dp[j] = max(dp[j], dp[j - k] + p);
      }
    }
  }

  int m;
  cin >> m;

  int w;
  vector<int> ans;
  for (int i = 0; i < m; i++)
  {
    cin >> w;
    if (dp[w] == 0)
    {
      cout << -1 << endl;
      return 0;
    }
    ans.push_back(dp[w]);
  }
  for (int x : ans)
    cout << x << endl;
  return 0;
}