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 <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

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

    vector<int> dp(393+1, -1);
    dp[0] = 0;
    for(int i=0; i<393; ++i){
        if(dp[i] == -1)
            continue;
        for(int j=0; j<n; ++j){
            for(int k=a[j]; k<=b[j]; ++k){
                int t = i + k;
                if(t <= 393)
                    dp[t] = max(dp[t], dp[i] + p[j]);
            }
        }
    }

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

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

    return 0;
}

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

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

int main(){
  //  pair<int ,int> P;


  vector<pair<int,int> > v;

  for(int i = 0; i < 400;i++){
    dp[i] = -1;
  }
  dp[0] = 0;

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

  for(int i = 1; i < 400;i++){
    int max = -1;
    for(int j = 0; j <= (i)/2; j++){
      if(dp[j] != -1 && dp[i-j] != -1){
	if(max < dp[j] + dp[i-j]) max =  dp[j] + dp[i-j];
      } 
    }
    dp[i] = max;
  }

  int m;
  cin >> m;
  vector<int> ans;
  int f = 0;
  for(int i = 0; i < m;i++){
    cin >> w;
    if(dp[w] == -1){
      cout << "-1\n";
      f = 1;
    }
    else{
      ans.push_back(dp[w]); 
    }
  }

  if(f != 1){
    for(int i = 0; i < ans.size() ;i++){
      cout << ans[i] << endl;
    }
  }
  return 0;
}

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

int main()
{
	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 <iostream>
#include <algorithm>
using namespace std;
#define MAX 400

int main() {
  int n;
  cin >> n;
  int s[n], l[n], p[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> l[i] >> p[i];
  }
  int dp[n + 1][MAX];
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < MAX; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < MAX; j++) {
      if (j < s[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        for (int k = l[i]; k >= s[i]; k--) {
          if (j - k < 0) continue;
          dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - k] + p[i]);
        }
      }
    }
  }
  int m;
  cin >> m;
  int ans[m];
  for (int i = 0; i < m; i++) {
    int w;
    cin >> w;
    if (dp[n][w] == 0) {
      cout << -1 << endl;
      return 0;
    }
    ans[i] = dp[n][w];
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << endl;
  }
  /*
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 10; j++) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdlib>

using namespace std;

#define ALL(c) c.begin(),c.end()
#define RALL(c) c.rbegin(),c.rend()
#define REP(type,i,n) for(type i=0;i<(n);++i)
#define MP(a,b) make_pair((a),(b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int,int> Pi;

const int INF=100000000;
const long long int INF_=1000000000000000000;

int main(){
	int N;
	cin >> N;
	vector<int> m(394);
	REP(int,i,N){
		int s,l,p;
		cin >> s >> l >> p;
		for(int j=s; j<=l; ++j) m[j]=max(m[j],p);
	}
	REP(int,i,394){
		REP(int,j,i+1){
			m[i]=max(m[i],m[i-j]+m[j]);
		}
	}
	//REP(int,i,9) cout << m[i] << ' ';
	cout << endl;
	int M;
	cin >> M;
	vector<int> ans;
	bool flag=true;
	REP(int,i,M){
		int w;
		cin >> w;
		if(m[w]==0) flag=false;
		else ans.push_back(m[w]);
	}
	if(!flag) cout << "-1" << endl;
	else REP(int,i,ans.size()) cout << ans[i] << endl;
	return 0;
}

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

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

  int sum=0;

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

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

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

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