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>
 
using namespace std;

#define MAX (393+1)

int main(){
 
	int n;
	int s,l,p;
	int m;
	int w;
	int dp[MAX];
	bool f;
	int ans[MAX];

	cin>>n;

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

	dp[0]=0;

	for(int i=0;i<n;i++){
		cin>>s>>l>>p;
		f=true;
		while(f){
			f=false;
			for(int j=s;j<=l;j++){
				for(int k=0;k<MAX-j;k++){
					if(dp[k]>=0){
						if(dp[k+j]<dp[k]+p){
							dp[k+j]=dp[k]+p;
							f=true;
						}
					}
				}
			}
		}
	}

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

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

	return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int s[400], l[400], p[400], w[400];
int dp[401];
/*
printf("Here\n");
*/

int main() {
	int n, m;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d %d %d", &s[i], &l[i], &p[i]);
	}
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		scanf("%d", &w[i]);
	}
	for (int i = n-1; i >= 0; i--) {
		for (int j = 0; j < 400; j++) {
			for (int k = s[i]; k <= l[i]; k++) {
				if (j-k>=0) {
					dp[j] = max(dp[j], dp[j-k]+p[i]);
				}
			}
		}
	}
	bool sw = false;
	for (int i = 0; i < m; i++) {
		if (dp[w[i]]==0) {
			sw = true;
			break;
		}
	}
	if (sw) {
		printf("-1\n");
	} else {
		for (int i = 0; i < m; i++) {
			printf("%d\n", dp[w[i]]);
		}
	}
	return 0;
}

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

class dp{
public:
  int n;
  static const int wlimit=10;
  int *s, *l, *p;
  int (*d)[wlimit+1];
  bool (*can)[wlimit+1];
  dp(int n):n(n){
    s = new int[n+1];
    l = new int[n+1];
    p = new int[n+1];
    d = new int[n+1][wlimit+1];
    can = new bool[n+1][wlimit+1];
  }
  ~dp(){
    delete[] s;
    delete[] l;
    delete[] p;
    delete[] d;
  }

  void init(){
    for(int i=0; i<=n; i++){
      for(int j=0; j<=wlimit; 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<=wlimit; j++){
        int temp=0;
        for(int k=s[i]; k<=l[i]; k++){
          if(j<k) temp = max(temp, d[i-1][j]);
          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 <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <complex>
#define repi(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define endl "\n"
#define ing long long
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2>& p) { os << p.first << " " << p.second; return os; }
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) { for(int i = 0; i < (int) v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; }

#define MAX 400

#define s first.first
#define l first.second
#define p second

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    vector<int> w;
    vector<pair<pair<int,int>,int>> in;
    vector<vector<int>> dp;
    
    cin>>n;

    in.resize(n);

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

    cin>>m;
    
    w.resize(m);

    dp.resize(2,vector<int>(MAX*MAX,0));

    dp[0][0] = 0;
    
    for(int i = 0; i < n; i++){
        for(int j = 0; j < MAX; j++){
            for(int k = in[i].s; k <= in[i].l ; k++){
                int num = j + k;
                if(num < MAX) dp[0][num] = max(dp[0][num], dp[0][j] + in[i].p);

                //dp[0][num] = max(dp[0][num], dp[0][num]);
            }
        }
    }


    
    for(int i = 0; i < m; i++){
        cin>>w[i];

        if(dp[0][w[i]] == 0){

            cout<<-1<<endl;
            return 0;
        }
    }
    for(int i = 0; i < m; i++){
        cout<<(dp[0][w[i]]?dp[0][w[i]]:-1)<<endl;
    }

    return 0;
}