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++: /* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type Abbreviation ---------- */
using ll = long long;

/* ---------- Constants ---------- */
const ll MOD = 1e9 + 7;
// const int INF = 1 << 25;
// const ll INF = 1LL << 50;
// const double PI = acos(-1);
// const double EPS = 1e-10;
// mt19937 mert(LL(time(0)));

/* ---------- Short Functions ---------- */
template <typename T>
T gcd(T a, T b) {
    if (a > b) return gcd(b, a);
    return a == 0 ? b : gcd(b % a, a);
}

template <typename T, typename U>
T mypow(T b, U n) {
    if (n == 0) return 1;
    if (n == 1) return b /* % MOD */;
    if (n % 2 == 0) {
        return mypow(b * b /* % MOD */, n / 2);
    } else {
        return mypow(b, n - 1) * b /* % MOD */;
    }
}

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */

int main() {
    int n;
    cin >> n;

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

    int N = 0;
    for (int i = 0; i < n; i++) {
        N += l[i] - s[i] + 1;
    }

    int weights[N];
    int values[N];
    int pnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = s[i]; j <= l[i]; j++) {
            values[pnt] = p[i];
            weights[pnt++] = j;
        }
    }

    int M;
    cin >> M;

    int W[M];
    for (int m = 0; m < M; m++) {
        cin >> W[m];
    }

    int dp[400];
    fill(dp, dp + 400, 0);
    for (int i = 0; i < N; i++) {
        for (int j = weights[i]; j < 400; j++) {
            dp[j] = max(dp[j], dp[j - weights[i]] + values[i]);
        }
    }

    int ret[M];
    fill(ret, ret + M, 0);

    for (int m = 0; m < M; m++) {
        if (dp[W[m]] == 0) {
            cout << -1 << endl;
            return 0;
        } else {
            ret[m] = dp[W[m]];
        }
    }

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

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
using ll = long long;
using namespace std;

int s[401], l[401], p[401], w[401];

int dp[401][401]; //indexの番号 重さ 最大コストをもつ
int const MOD = 1e9 + 7;
int main(void)
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> s[i] >> l[i] >> p[i];

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

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= 393; ++j)
        {
            for (int k = s[i]; k <= l[i]; ++k)
            {
                if (j - k >= 0)
                {
                    dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - k] + p[i]);
                }
                else
                {
                    dp[i + 1][j] = dp[i][j];
                }
            }
        }
    }

    bool ok = true;
    vector<int> ans;
    for (int i = 0; i < m; ++i)
    {
        if (dp[n][w[i]] == 0)
            ok = false;
        ans.push_back(dp[n][w[i]]);
    }
    if (!ok)
        cout << -1 << endl;
    else
    {
        for (auto e : ans)
            cout << e << endl;
    }
    return 0;
}

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

int dp[400];
int n,s,l,p,w,ans[400];

int main(){

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

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

  for(int i = 1; i < 400;i++){
    int max = -1;
    for(int j = 0; j <= (i)/2; j++){
      //  if(dp[j] != -1 && dp[i-j] != -1){
	if(max < dp[j] + dp[i-j]) max =  dp[j] + dp[i-j];
      } 
    //   }
    dp[i] = max;
  }

  for(int i = 0; i < 393 ;i++){
    //  cout << dp[i] << " ";
  }
  //    cout << endl;



  int m;
  cin >> m;
  vector<int> ans;
  int f = 0;
  for(int i = 0; i < m;i++){
    cin >> w;
    if(dp[w] == -1){
      cout << "-1\n";
      f = 1;
    }
    else{
      ans.push_back(dp[w]); 
    }
  }

  if(f != 1){
    for(int i = 0; i < ans.size() ;i++){
      cout << ans[i] << endl;
    }
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#define N 393
using namespace std;
void func(int,int,int);
int n,s[N],l[N],p[N],m,w,a,ans[N],flag,flag2;

int main(){
  cin>>n;
  for(int i=0;i<n;i++) cin>>s[i]>>l[i]>>p[i];
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    a=-1;
    flag2=0;
    func(0,0,0);
    if(!flag2) a=-1;
    if(a==-1) flag=1;
    else ans[i]=a;
  }
  if(flag) cout<<-1<<endl;
  else{
    for(int i=0;i<m;i++) cout<<ans[i]<<endl;
  }
  return 0;
}

void func(int x,int len,int point){
  if(x==n){
    a=max(a,point);
    return ;
  }
  func(x+1,len,point);
  for(int i=s[x];i<=l[x];i++){
    if(len+i>w) continue;
    flag2=1;
    func(x+1,len+i,point+p[x]);
  }
}

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

int main(void)
{
	int n, s[393], l[393],p[393];
	cin >> n;
	for(int i = 0; i < n; i++) cin >> s[i] >> l[i] >> p[i];
	int m, w[393];
	cin >> m;
	int longest = 0;
	for(int i = 0; i < m; i++){
		cin >> w[i];
		longest = max(longest,w[i]);
	}
	
	int point[394][394] = {0};
	for(int j = 0; j < n; j++){//j番目のフレーズ
		for(int k = 0; k < longest; k++) point[j + 1][k] = point[j][k];

		for(int k = 0; k < longest; k++){
			if(k == 0 || point[j][k]){
				for(int x = s[j]; x <= l[j]; x++){
					if(k + x <= longest){
						point[j + 1][k + x] = max(point[j + 1][k + x], point[j][k] + p[j]);
					}
				}
			}
		}
	}

	int max[393];
	for(int i = 0; i < m; i++){//i番目のメロディー
		if(point[n][w[i]]) max[i] = point[n][w[i]];
		else {max[0] = -1; break;}
	}


	if(max[0] == -1) cout << -1 << endl;
	else{
		for(int i = 0; i < m; i++){
			cout << max[i] << endl;
		}
	}
}