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<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include <algorithm> 
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout<<(x)<<endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,vector<ll>,greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define mp make_pair
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define sz(x) (ll)(x).size()
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
using vc = vector<char>;
using vvc = vector<vc>;
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; 
 }
ll dx[4]={0,1,0,-1};
ll dy[4]={1,0,-1,0};
ll dp[400][400];
int main(){
  ll n,m;
  cin>>n;
  memset(dp,-1,sizeof(dp));
  dp[0][0]=0;
  vll s(n),l(n),p(n);
  rep(i,n){
    cin>>s[i]>>l[i]>>p[i];
  }
  cin>>m;
  vll w(m);
  rep(i,m) cin>>w[i];
  rep(i,n){
    rep(j,397){
      dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
      for(int k=s[i];k<=l[i];k++){
        if(j>=k&&dp[i+1][j-k]>=0){
          dp[i+1][j]=max(dp[i+1][j-k]+p[i],dp[i+1][j]);
        }
      }
    }
  }
  rep(i,m){
    if(dp[n][w[i]]<0){
      COUT(-1);
      return 0;
    }
  }
  rep(i,m){
    COUT(dp[n][w[i]]);
  }
}

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;
	while(m--){
		int input;
		cin >> input;
		if(dp[input]) cout << dp[input] << endl;
		else cout << "-1" << endl;
	}
	return 0;
}

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

int n, m, s, l, p, w;
int memo[400] = {0};
int dp[400][400] = {0};
int ans[400] = {0};
bool able = 1;

int main() {
  int i, j, k;
  cin >> n;
  for(i = 0; i <= 393; ++i) memo[i] = -1;

  for(i = 0; i < n; ++i) {
    cin >> s >> l >> p;
    for(j = s; j <= l; ++j) memo[j] = max(memo[j], p);
  }
  cin >> m;
  for(k = 0; k < m; ++k) {
    cin >> w;
    for(i = 1; i <= 393; ++i) {
      for(j = 0; j <= w; ++j) {
        dp[i][j] = dp[i - 1][j];
        if(i <= j)
          dp[i][j] = max(dp[i][j], dp[i][j - i] + memo[i]);
      }
    }
    if(dp[393][w] == 0) {
      able = 0;
      break;
    }
    else
      ans[k] = dp[393][w];
  }
  if(able)
    for(i = 0; i < m; ++i) cout << ans[i] << endl;
  else
    cout << -1 << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,a[394],b[394],c[394];
int dp[394][394];

int main(){
  cin>>n;
  for(int i=1;i<=n;i++)cin>>a[i]>>b[i]>>c[i];
  memset(dp,-1,sizeof(dp));
  dp[0][0]=0;

  for(int i=1;i<=n;i++){
    deque<int> deq;    
    for(int j=0;j<=393;j++){      
      dp[i][j]=dp[i-1][j];
      
      for(int k=a[i];k<=b[i];k++){
	if(j-k>=0&&dp[i][j-k]>=0)dp[i][j]=max(dp[i][j],dp[i][j-k]+c[i]);
      }
      /*
      int key=j-a[i];
      if(key>=0&&dp[i-1][key]>=0){
	while(!deq.empty()&&dp[i-1][deq.back()]<=dp[i-1][key])deq.pop_back();
	deq.push_back(key);
      }
      
      while(!deq.empty()&&deq.front()<j-b[i])deq.pop_front();
      if(!deq.empty())dp[i][j]=max(dp[i][j],dp[i-1][deq.front()]+c[i]);
      */
    }
  }

  int q,Q;
  cin>>Q;
  while(Q--){
    cin>>q;
    cout<<dp[n][q]<<endl;
  }
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long

const long long MOD = 1000000007;

int main(){
  int n,m;
  cin >> n;
  vector<int> s(n),l(n),p(n);
  rep(i,n) cin >> s.at(i) >> l.at(i) >> p.at(i);

  cin >> m;
  int mw=0;
  vector<int> w(m);
  rep(i,m){
      cin >> w.at(i);
      mw = max(mw,w.at(i));
  }

  vector<vector<int>> dp(n+1,vector<int>(mw+1,0));

  rep(i,n){
      rep(j,mw+1){
        if(j<s.at(i)) dp.at(i+1).at(j) = dp.at(i).at(j);
        else{
            for(int k=s.at(i);k<=l.at(i);k++){
                if(j<k) break;
                else dp.at(i+1).at(j) = max(dp.at(i+1).at(j),max(dp.at(i).at(j),dp.at(i+1).at(j-k)+p.at(i)));
            }
        }
      }
  }

  bool ans = true;
  rep(i,m){
      if(dp.at(n).at(w.at(i))==0) ans=false;
  }

  if(ans){
      rep(i,m) cout << dp.at(n).at(w.at(i)) << endl;
  }
  else cout << -1 << endl;

}

C++: #include<stdio.h>
int main(){
  int n,s[400],l[400],p[400],m,w,i,j,k,dp[400][400],ans[400],flag=0;
  scanf("%d\n",&n);
  for(i=1;i<=n;i++){
    scanf("%d %d %d\n",&s[i],&l[i],&p[i]);
  }
  for(i=0;i<=n;i++){
    for(j=0;j<=393;j++){
      dp[i][j]=0;
    }
  }
  for(i=1;i<=n;i++){
    for(k=s[i];k<=l[i];k++){
      for(j=0;j<=393;j++){
        if(j-k>=0){
          if(dp[i][j-k]+p[i]>dp[i][j]){
            dp[i][j]=dp[i][j-k]+p[i];
          }
          if(dp[i-1][j]>dp[i][j]){
            dp[i][j]=dp[i-1][j];
          }
        }
        else{
          if(dp[i-1][j]>dp[i][j]){
            dp[i][j]=dp[i-1][j];
          }
        }
      }
    }
  }
  scanf("%d\n",&m);
  for(i=0;i<m;i++){
    scanf("%d\n",&w);
    if(dp[n][w]==0){
      flag=1;
      break;
    }
    else{
      ans[i]=dp[n][w];
    }
  }
  if(flag==1){
    printf("-1\n");
  }
  else{
    for(i=0;i<m;i++){
      printf("%d\n",ans[i]);
    }
  }
  return 0;
}

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

using namespace std;

int main(){
	int n,m;
	cin >> n;
	int s,l,p;
	int w[400]={0,};
	for(int i=0;i<n;i++){
		cin >> s >> l >> p;
		for(int j=s;j<=l;j++){
			for(int k=0;k+j<=399;k++){
				w[k+j]=max(w[k+j],w[k]+p);
			}
		}
	}
	cin >> m;
	for(int i=0;i<m;i++){
		cin >> s;
		if(w[s])cout << w[s] << endl;
		else cout << -1 << endl;
	}
}