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

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

class dp{
public:
  int n;
  static const int limit=393;
  int s[limit+1], l[limit+1], p[limit+1];
  int d[limit+1][limit+1];
  bool can[limit+1][limit+1];

  dp(int n):n(n){}

  void init(){
    for(int i=0; i<=n; i++){
      for(int j=0; j<=limit; j++) {
        d[i][j] = 0;
        if(j==0) can[i][j] = true;
        else can[i][j] = false;
      }
    }
    for(int i=1; i<=n; i++){
      for(int j=0; j<=limit; j++){
        int temp=0;
        for(int k=s[i]; k<=l[i]; k++){
          if(j<k) {
            temp = max(temp, d[i-1][j]);
            if(can[i-1][j]) can[i][j] = true;
          }
          else {
            if(can[i][j-k]){
              temp = max(temp, max(d[i-1][j], d[i][j-k]+p[i]));
              can[i][j] = true;
            }
          }
        }
        d[i][j] = temp;
      }
    }
  }

  vector<int> ans(vector<int> w){
    vector<int> ans;
    for(int i=0; i<w.size(); i++){
      if(d[n][w[i]]==0){
        ans = vector<int>(1, -1);
        return ans;
      }
      ans.push_back(d[n][w[i]]);
    }
    return ans;
  }
};

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

  int s, l, p;
  for(int i=1; i<=n; i++){
    cin >> s >> l >> p;
    d.s[i] = s;
    d.l[i] = l;
    d.p[i] = p;
  }

  d.init();

  int m; cin >> m;
  vector<int> melodies;
  for(int i=0; i<m; i++){
    int w; cin >> w;
    melodies.push_back(w);
  }

  vector<int> ans = d.ans(melodies);
  for(int i=0; i<ans.size(); i++){
    cout << ans[i] << endl;
  }

  return 0;
}