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

C++: // aribon2-3-3_a
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))
#define ALL(x) x.begin(), x.end()

#define INF ((1 << 29)-1)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
template<class T> void print(const T& x){cout << x << endl;}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

int dp[394];

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt");
    cin.rdbuf(in.rdbuf());
#endif

    int N;
    cin >> N;

    vector<P> sl(N);
    vector<int> p(N);
    REP(i, N) cin >> sl[i].first >> sl[i].second >> p[i];

    int M;
    cin >> M;
    vector<int> Ws(M);
    REP(i, M) cin >> Ws[i];

    REP(j, 394) dp[j] = -1;
    dp[0] = 0;
    REP(i, N){
        int s = sl[i].first;
        int l = sl[i].second;
        REP(j, 394){
            if (dp[j] == -1) continue;
            REPN(k, s, l+1){
                if (j + k > 393) continue;
                dp[j+k] = max(dp[j+k], dp[j] + p[i]);
            }
        }
    }

    REP(i, M){
        if (dp[Ws[i]] == -1) {
            print(-1);
            return 0;
        }
    }

    REP(i, M) {
        print(dp[Ws[i]]);
    }

    return 0;
}

C++: #include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<limits.h>
#include<utility>
#include<bits/stdc++.h>
#include<vector>
#include<tuple>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define ALL(A) A.begin(),A.end()


int main() {
	int n;
	cin >> n;
	int a[n][3];
	rep(i, n) {
		cin >> a[i][0] >> a[i][1] >> a[i][2];
	}
	int m;
	cin >> m;
	int p[m];
	int b;
	vector<int> point(394,INT_MIN);
	rep(i, m) {
		cin >> b;
		p[i] = b;
		if (point[b] == INT_MIN) {
			vector<int> c(b + 1, INT_MIN);

			c[0] = 0;

			rep(j, b) {
				rep(k, n) {
					for (int l = a[k][0]; l <= a[k][1]; l++) {
						if (b >= l + j) {
							if (c[l + j] < c[j] + a[k][2]) {
								c[l + j] = c[j] + a[k][2];
							}
						}
					}
				}
			}
			if (c[b] == INT_MIN) {
				cout << -1 << endl;
				return 0;
			}
			//p[i] = c[b];
			point[b] = c[b];
		}
	}
	rep(i, m) {
		cout << point[p[i]] << endl;
	}
}