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++: // C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  bool operator< (const Point &p1) {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}

struct Phrase {
  Int l, p;

  bool operator < (const Phrase &other) const {
    return (l == other.l) ? p < other.p : l < other.l;
  }
};

#define MAX_W 394
#define MAX_N 394
Int N, M;

vector<Phrase> phrases_;
vector<Phrase> phrases;
vector<Int> melos;


void input() {
  Int s, l, p;
  Phrase ph;
  cin >> N;
  rep(N) {
    cin >> s >> l >> p;
    loop(x,s,l+1) {
      ph.l = x, ph.p = p;
      phrases_.push_back(ph);
    }
  }
  cin >> M;
  rep(M) {
    cin >> s;
    melos.push_back(s);
  }

  sort(span_all(phrases_));
  reverse(span_all(phrases_));
  Int prev_l = -1;
  for (auto ph: phrases_) {
    if (prev_l == ph.l) continue;
    phrases.push_back(ph);
    prev_l = ph.l;
  }
  N = phrases.size();

  phrases_.clear();
}

vector<vector<Int> > dp(MAX_N, vector<Int>(MAX_W, -1));

Int dfs(Int n, Int cap) {
  if (n == N || cap == 0) return 0;
  if (dp.at(n).at(cap) >= 0) return dp.at(n).at(cap);
  Int val = dfs(n + 1, cap);
  Phrase ph = phrases.at(n);
  if (cap >= ph.l) {
    val = max(
      val,
      max(
        ph.p + dfs(n + 1, cap - ph.l),
        ph.p + dfs(n, cap - ph.l)
      )
    );
  }
  dp.at(n).at(cap) = val;
  return val;
}

void solve() {
  vector<Int> ans;
  for (auto melo: melos) {
    loop(i,0,MAX_N) loop(j,0,MAX_W) dp.at(i).at(j) = -1;
    Int score = dfs(0, melo);
    if (score == 0) {
      cout << -1 << endl;
      return;
    }
    ans.push_back(score);
  }

  for (auto a: ans) cout << a << endl;
}

int main() {
  input();
  solve();
  return 0;
}

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

C++: //b
#include <iostream>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <memory>
#include <functional>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>

#define REP(i,b,n) for(int i=b;i<n;i++)
#define REPR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define EACH(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n>>b) & 1)
#define PB push_back
#define MP make_pair

using namespace std;

static const double PI = acos(-1.0);
static const double EPS = 1e-9;
typedef long long ll;
typedef pair<int, int> pii;

int n;
int s[400], l[400], p[400];
int seg = 0;
int m;
int w[400];
queue<int> res;

int dp[400][400];

bool solve(){
	
	for(int k=0; k<m; ++k){
		
		CLR(dp);
		
		for(int i=0; i<n; ++i){
			for(int j=0; j<=w[k]; ++j){
			
					if(j<s[i])
						dp[i+1][j] = dp[i][j];
					else
						dp[i+1][j] = max(dp[i][j], dp[i+1][j-s[i]] + p[i]);
			}
		}
		
		
		int ret = 0;
		
		for(int i=w[k]; i>=w[k]-seg; --i){
			if(dp[n][i] != 0)
				ret = max(ret, dp[n][i]);
		}
		
		if(ret == 0)
			return false;
		
		res.push(ret);
	}
	
	return true;
}

int main(){
	
	cin >> n;
	
	for(int i=0; i<n; ++i){
		cin >> s[i] >> l[i] >> p[i];
		seg += l[i]-s[i];
	}
	
	cin >> m;
	
	for(int i=0; i<m; ++i){
		cin >> w[i];
	}
	
	if(solve()==false)
		cout << -1 << endl;
	else{
		while(!res.empty()){
			cout << res.front() << endl;
			res.pop();
		}
	}
	
	return 0;
}