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

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