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++: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef pair<int, int> P;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const vector<int> di = {-1, 0, 1, 0};
const vector<int> dj = {0, -1, 0, 1};
const int INF = 1001001001;
const int MOD = 1000000007;

int n, m;
vector<tuple<int, int, int>> phrases;
vector<int> melodies;

int main() {
  cin >> n;
  phrases.resize(n);

  rep(i, n) {
    int s, l, p;
    cin >> s >> l >> p;
    phrases[i] = make_tuple(s, l, p);
  }

  cin >> m;
  melodies.resize(m);
  rep(i, m) cin >> melodies[i];

  vector<int> ans(m);
  bool ok = true;

  rep(i, m) {
    int melody_length = melodies[i];
    vector<vector<int>> dp(n + 1, vector<int>(melody_length + 1, 0));

    rep(j, n) {
      int shortest, longest, score;
      tie(shortest, longest, score) = phrases[j];

      rep(k, melody_length + 1) {
        dp[j + 1][k] = dp[j][k];

        if (k - shortest >= 0) {
          chmax(dp[j + 1][k], dp[j + 1][k - shortest] + score);
        }

        if (k - longest >= 0) {
          chmax(dp[j + 1][k], dp[j + 1][k - longest] + score);
        }
      }
    }

    // for (auto&& x : dp) {
    //   for (auto&& y : x) {
    //     cout << y << ' ';
    //   }
    //   cout << endl;
    // }

    if (dp[n][melody_length]) {
      ans[i] = dp[n][melody_length];
    } else {
      ok = false;
      break;
    }
  }

  if (ok) {
    for (auto&& i : ans) {
      printf("%d\n", i);
    }
  } else {
    printf("-1\n");
  }
}

C++: #include <algorithm>
#include <iostream>
#define N 394
using namespace std;

int main(){
  int n,s[N],l[N],p[N],m,w,a[N],f=0,dp[N];
  cin>>n;
  for(int i=0;i<n;i++) cin>>s[i]>>l[i]>>p[i];
  for(int i=0;i<N;i++) dp[i]=-1;
  dp[0]=0;
  for(int i=0;i<n;i++)
    for(int j=0;j<N;j++)
      for(int k=s[i];k<=l[i];k++)
	if(j-k>=0&&dp[j-k]!=-1) dp[j]=max(dp[j],dp[j-k]+p[i]);
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>w;
    dp[w]==-1?f=1:a[i]=dp[w];
  }
  if(f) cout<<-1<<endl;
  else{
    for(int i=0;i<m;i++) cout<<a[i]<<endl;
  }
  return 0;  
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[2][401][401]={};
int main(){
  int n;
  cin>>n;
  int s[401],l[401],p[401];
  
  for(int i=0;i<n;i++)
    cin>>s[i]>>l[i]>>p[i];
  
  for(int i=0;i<n;i++){
    int nw=i%2;
    int nt=!nw;
    for(int j=0;j<400;j++)
      for(int k=0;k<400;k++)
	dp[nt][i][j]=0;
    for(int j=0;j<400;j++){
      for(int k=0;k<400;k++){
	int ny=min(s[i]+j,399);
	int nz=min(l[i]-s[i]+k,399);
	
	/*
	  if(i&&j-s[i-1]>=0&&k-(l[i-1]-s[i-1])>=0)
	  if(dp[i][j-s[i-1]][k-(l[i-1]-s[i-1])])
	    dp[i][j][k]=max(dp[i][j][k],dp[i][j-s[i-1]][k-(l[i-1]-s[i-1])]+p[i-1]);
	*/
	
	
	if(j+k==0){
	  dp[nt][ny][nz]=max(dp[nt][ny][nz],p[i]);
	  dp[nw][ny][nz]=max(dp[nw][ny][nz],dp[nw][j][k]+p[i]);
	}
	dp[nt][j][k]=max(dp[nt][j][k],dp[nw][j][k]);
	
	if(dp[nw][j][k]){
	  dp[nt][ny][nz]=max(dp[nt][ny][nz],dp[nw][j][k]+p[i]);
	  dp[nw][ny][nz]=max(dp[nw][ny][nz],dp[nw][j][k]+p[i]);
	}
      }
    }
  }
  int m,a[400],f=0;
  cin>>m;
  for(int i=0;i<m;i++){
    int w;
    cin>>w;
    int ans=0;
    for(int j=0;j<400;j++){
      for(int k=0;k<400;k++){
	if(j<=w&&j+k>=w)ans=max(ans,dp[n%2][j][k]);
      }
    }
    if(ans==0)f=1;
    a[i]=ans;
  }
  if(f)cout<<-1<<endl;
  else for(int i=0;i<m;i++)cout<<a[i]<<endl;
  return 0;
}

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

int point[393];
int dp[394];
pair<int,int>len[393];
int n;

void check(int mmin,int max,int i,int pp)
{
  // cout<<mmin<<" "<<max<<" "<<i<<" "<<pp<<endl;
  if(mmin>393||i>=n)
    return;
  int vmax=min(max,393);
  bool cnt=false;
  for(int j=mmin;j<=vmax;j++)
    {
      if(dp[j]<pp)
	{
	  dp[j]=pp;
	  cnt=true;
	}
    }
//  if(!cnt)
//    return;
  check(mmin,max,i+1,pp);
  check(mmin+len[i].first,max+len[i].second,i,pp+point[i]);
}

int main()
{
 
  cin>>n;
  for(int i=0;i<n;i++)
    {
      int s,l,p;
      cin>>s>>l>>p;
      len[i]=make_pair(s,l);
      point[i]=p;
    }
  int m;
  cin>>m;
  memset(dp,-1,sizeof(dp));
  check(0,0,0,0);
  for(int i=0;i<m;i++)
    {
      int w;
      cin>>w;
      cout<<dp[w]<<endl;
    }
}

C++: #include <iostream>
#include <vector>

using namespace std;

int MAX = 393;

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

	vector<int> S(n), L(n), P(n);//, SUB(n);
	for (int i=0; i<n; i++) {
		cin >> S[i] >> L[i] >> P[i];
		//SUB[i] = L[i]-S[i];
	}

	int m;
	cin >> m;

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

	int **dp = new int*[MAX+10];
	for (int i=0; i<MAX+10; i++) {
		dp[i] = new int[3*MAX];
		for (int j=0; j<3*MAX; j++)
			dp[i][j] = 0;
	}

	dp[0][0] = 0;
	for (int j=S[0]; j<=L[0]; j++)
		dp[0][j] = P[0];
	
	for (int i=1; i<n; i++) {
		for (int j=0; j<=MAX; j++) {
			dp[i][j] = dp[i-1][j];
			if (j>=S[i])
				for (int k=S[i]; k<=L[i]; k++)
					if (j-k>=0) dp[i][j] = max(dp[i][j], dp[i][j-k]+P[i]);
		}
	}

	vector<int> point(m);
	bool flag = false;
	for (int i=0; i<m; i++) {
		point[i] = dp[n-1][W[i]];
		if (point[i]==0) {
			flag = true;
			break;
		}
	}
	
	if (flag) cout << -1 << endl;
	else {
		for (int i=0; i<m; i++)
			cout << point[i] << endl;
	}

	for (int i=0; i<MAX+10; i++)
		delete [] dp[i];
	delete [] dp;

	return 0;
}