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 <string>
#include <vector>
#include <algorithm>
#include <climits>
#include <cmath>

using namespace std;

int main() {
	int n;

	cin >> n;

	vector< int > s, l, p;

	for(int i = 0; i < n; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		s.push_back(a);
		l.push_back(b);
		p.push_back(c);
	}

	vector< int > dp(394, 0);
	for(int i = 0; i < n; i++) {
		dp[s[i]] = p[i];
		dp[l[i]] = p[i];
	}

	int m;
	cin >> m;
	vector< int > w;

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

	for(int i = 0; i < m; i++) {
		bool flag = true;
		for(int j = 1; j <= 393; j++) {
			for(int k = 0; k < n; k++) {
				dp[j + s[k]] = max(dp[j + s[k]], dp[j] + p[k]);
				dp[j + l[k]] = max(dp[j + l[k]], dp[j] + p[k]);
				if(j + s[k] > w[i]) {
					flag = false;
					break;
				}
			}
			if(!flag) {
				break;
			}
		}
	}

	bool flag = true;
	for(int i = 0; i < m; i++) {
		if(dp[w[i]] == 0) {
			flag = false;
		}
	}

	for(int i = 0; i < m; i++) {
		if(flag) {
			cout << dp[w[i]] << endl;
		} else {
			cout << "-1" << endl;
			break;
		}
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,a,b,c;
int t[500];
int dp[500];
int main(){
  memset(t,-1,sizeof(t));
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a>>b>>c;
    for(int j=a;j<=b;j++)t[j]=max(t[j],c);    
  }
  memset(dp,-1,sizeof(dp));
  dp[0]=0;
  for(int j=1;j<=500;j++)
    for(int i=500;i>=1;i--)
      if(t[j]>=0&&i-j>=0&&dp[i-j]>=0)dp[i]=max(dp[i],dp[i-j]+t[j]);

  cin>>a;
  while(cin>>a)cout<<dp[a]<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
inline void chmax (int& a, int b) {
  if (a < b) a = b;
}
void err() {
  cout << -1 << '\n';
  exit(0);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> dp(394, -1);
  dp[0] = 0;
  while (n--) {
    int s, l, p;
    cin >> s >> l >> p;
    for (int i = 393; i >= s; i--) {
      int &crr = dp[i];
      for (int j = i - s; j >= max(0, i - l); j--) {
        int &prv = dp[j];
        if (prv == -1) continue;
        chmax(crr, prv + p);
      }
    }
  }
  int q;
  cin >> q;
  vector<int> que;
  while (q--) {
    int w;
    cin >> w;
    int ret = dp[w];
    if (ret == -1) err();
    que.push_back(ret);
  }
  for (int ret : que) {
    cout << ret << '\n';
  }
  return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> P;
struct pos {
  int row;
  int column;
};
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1 << 30;
const ll MOD = 1000000007;
struct Phrase {
  int min;
  int max;
  int point;
};
int main(void) {
  int P_SIZE, M_SIZE;
  Phrase ph[400];
  int ml[400];
  cin >> P_SIZE;
  rep(i, P_SIZE) { cin >> ph[i].min >> ph[i].max >> ph[i].point; }
  cin >> M_SIZE;
  rep(i, M_SIZE) { cin >> ml[i]; }

  vector<int> result;

  rep(i, M_SIZE) {
    int melo = ml[i];
    int dp[401][401];
    memset(dp, 0, sizeof(dp));
    rep(p, P_SIZE) {
      auto phra = ph[p];
      for (int m = 0; m <= melo; m++) {
        if (phra.min > m) {
          dp[p + 1][m] = dp[p][m];
        } else {
          dp[p + 1][m] =
              max(dp[p + 1][m],
                  max(dp[p][m], dp[p + 1][m - phra.min] + phra.point));
        }
      }
    }

    if (dp[P_SIZE][melo] == 0) {
      cout << -1 << endl;
      return 0;
    } else {
      result.push_back(dp[P_SIZE][melo]);
    }
  }

  rep(i, result.size()) { cout << result[i] << endl; }

  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

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

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<iomanip>
#include<functional>
using namespace std;

int MOD = 1e9 + 7;

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

int main() {
	int n;
	cin >> n;
	vector<int> s(n + 1), l(n + 1), p(n + 1);
	for (int i = 1; i <= n; i++) cin >> s[i] >> l[i] >> p[i];
	int m;
	cin >> m;
	vector<int> w(m + 1);
	for (int i = 1; i <= m; i++) cin >> w[i];

	vector<vector<int>> dp(400, vector<int>(400, 0));
	vector<int> ans(m + 1);

	for (int k = 1; k <= m; k++) {
		int MAX = 0;

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= w[k]; j++) {

				if (j >= s[i]) {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - s[i]] + p[i]);
				}
				else {
					dp[i][j] = dp[i - 1][j];
				}
			}
		}

		for (int i = 1; i <= w[k]; i++) {
			MAX = max(MAX, dp[n][i]);
		}

		if (MAX == 0) {
			cout << -1 << endl;
			return 0;
		}

		ans[k] = MAX;
	}

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

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

int main(int argc, char *argv[]){
  int n, m;
  cin >> n;
  vector<int> s(n), l(n), p(n);
  vector<vector<int> > dp(n + 1, vector<int>(394, -1));
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> l[i] >> p[i];
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 394; j++) {
      for (int k = s[i]; k <= l[i]; k++) {
        if(j < k){
          dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);        
        }else{
          if(dp[i + 1][j - k] == -1 &&
             dp[i][j] == -1)continue;
          dp[i + 1][j] = max(dp[i + 1][j],
                             max(dp[i][j],
                                 dp[i + 1][j - k] + p[i]));
        }
      }
    }
  }
  cin >> m;
  int query;
  vector<int> ans;
  for (int i = 0; i < m; i++) {
    cin >> query;
    ans.push_back(dp[n][query]);
    if(ans.back() == -1){
      std::cout << -1 << std::endl;
      return 0;
    }
  }
  for (int i: ans) {
    std::cout << i << std::endl;
  }
  return 0;
}