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;

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

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int n,s,l,p,m,w;
int dp[400];
int ans[400];

int main(){
  cin >> n;
  memset(dp,-1,sizeof(dp));
  dp[0] = 0;

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

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

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

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

using namespace std;

const int N = 393;

int n, s[N], l[N], p[N], m, ans[N], f, dp[N+1], w;

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

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

const int len = 393;
int main(){
  int n;
  cin >> n;
  vector<vi> dp(n+1,vi(len+1,-1));
  dp[0][0] = 0;

  rep(i,n){
    int s,l,p;
    cin >> s >> l >> p;
    rep(j,len+1){
      dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
      for(int t=s;t<=l;t++){
	if(j+t<=len){
	  dp[i+1][j+t] = max(dp[i+1][j+t], dp[i][j] + p);
	}
      }
    }
  }

  int m;
  cin >> m;
  vector<int> ans(m);
  rep(i,m){
    int w;
    cin >> w;
    if(dp[n][w]<0){
      cout << -1 << endl;
      return 0;
    }
    ans[i] = dp[n][w];
  }
  rep(i,m)cout << ans[i] << endl;
}