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<string>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<iomanip>
#include<functional>
using namespace std;

int MOD = 1e9 + 7;

typedef long long ll;
typedef pair<int, int> P;

int main() {
	int n;
	cin >> n;
	vector<int> s(n + 1), l(n + 1), p(n + 1);
	for (int i = 1; i <= n; i++) cin >> s[i] >> l[i] >> p[i];
	int m;
	cin >> m;
	vector<int> w(m + 1);
	for (int i = 1; i <= m; i++) cin >> w[i];

	vector<vector<int>> dp(400, vector<int>(400, 0));
	vector<int> ans(m + 1);

	for (int k = 1; k <= m; k++) {
		int MAX = 0;

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= w[k]; j++) {

				if (j >= s[i]) {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - s[i]] + p[i]);
				}
				else {
					dp[i][j] = dp[i - 1][j];
				}
			}
		}

		for (int i = 1; i <= w[k]; i++) {
			MAX = max(MAX, dp[n][i]);
		}

		if (MAX == 0) {
			cout << -1 << endl;
			return 0;
		}

		ans[k] = MAX;
	}

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

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