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<cstring>
#include<algorithm>
using namespace std;

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

int main(){
  cin >> n;
  memset(dp,-1,sizeof(dp));
  dp[0] = 0;

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

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

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

using namespace std;

const int N = 393;

int n, s[N], l[N], p[N], m, ans[N], f, dp[N+1], w;

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

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef vector<int> vi;

const int len = 393;
int main(){
  int n;
  cin >> n;
  vector<vi> dp(n+1,vi(len+1,-1));
  dp[0][0] = 0;

  rep(i,n){
    int s,l,p;
    cin >> s >> l >> p;
    rep(j,len+1){
      dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
      for(int t=s;t<=l;t++){
	if(j+t<=len){
	  dp[i+1][j+t] = max(dp[i+1][j+t], dp[i][j] + p);
	}
      }
    }
  }

  int m;
  cin >> m;
  vector<int> ans(m);
  rep(i,m){
    int w;
    cin >> w;
    if(dp[n][w]<0){
      cout << -1 << endl;
      return 0;
    }
    ans[i] = dp[n][w];
  }
  rep(i,m)cout << ans[i] << endl;
}

C++: #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=2*asin(1);

int DP[400][400];

int main() {
    int N;
    cin>>N;
    int s[N], l[N], p[N];
    for(int i=0;i<N;++i){
        cin>>s[i]>>l[i]>>p[i];
    }
    int M;
    cin>>M;
    int w[M];
    for(int i=0;i<M;++i){
        cin>>w[i];
    }
    bool can = true;
    int ans[M];
    for(int i=0;i<M;++i){
        for(int j=0;j<N;++j){
            for(int k=0;k<=w[i];++k){
                if(k==0) DP[j][k] = 0;
                else DP[j][k] = -1;
            }
        }
        for(int j=0;j<=w[i];++j){
            if(DP[0][j]>-1){
                for(int k=s[0];(k<=l[0])&&(j+k<=w[i]);++k){
                    DP[0][j+k] = DP[0][j] + p[0];
                }
            }
        }
        for(int j=1;j<N;++j){
            for(int k=0;k<=w[i];++k){
                if(k>=s[j]){
                    for(int a=s[j];(a<=l[j])&&(k-a>=0);++a){
                        if(DP[j][k-a]>-1) {
                            DP[j][k] = max(DP[j-1][k], max(DP[j][k], DP[j][k-a] + p[j]));
                            break;
                        }
                        else DP[j][k] = DP[j-1][k];
                    }
                }
                else{
                    DP[j][k] = DP[j-1][k];
                }
            }
        }
        if(DP[N-1][w[i]]==-1) can = false;
        else ans[i] = DP[N-1][w[i]];
    }
    if(can){
        for(int i=0;i<M;++i){
            cout<<ans[i]<<endl;
        }
    }
    else cout<<-1<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define LOOP(val,times) for(val = 0;val < (times);++val)

int main(){
    int dp[400];
    int n,W;
    int i,j;
    int weight,value;
    int inf,sup,score;
    scanf("%d",&n);
    LOOP(i,400) dp[i] = 0;
    LOOP(i,n){
        scanf("%d %d %d",&inf,&sup,&score);
        for(weight = inf;weight <= sup;weight++){
            bool flag = 1;
            while(flag){
                flag = 0;
                LOOP(j,400){
                    if(j >= weight && dp[j-weight] != 0 && dp[j] < dp[j-weight] + score){
                        flag = 1;
                        dp[j] = dp[j-weight] + score;
                    }
                    if(j == weight){
                        dp[j] = max(score,dp[j]);
                    }
                }
            }
        }
    }
    int ans[400];
    int m;
    scanf("%d",&m);
    LOOP(i,m){
        scanf("%d",&W);
        if(dp[W] == 0){
            printf("%d\n",-1);
            return 0;
        }
        ans[i] = dp[W];
    }
    LOOP(i,m){
        printf("%d\n",ans[i]);
    }
    int k;cin >> k;
}

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

int n, m, s[400], l[400], p[400];
// dp[i][j] := i番目までのメロディを使っていて長さがjのときの最高点数
int dp[400][400];

int main(){
	for(int i = 0; i < 400; i++){
		for(int j = 0; j < 400; j++){
			dp[i][j] = -1;
		}
	}
	
	cin >> n;
	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 < 400; j++){
			if( dp[i][j] == -1 ) continue;
			
			dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
			for(int k = j + s[i]; k <= j + l[i]; k++){
				dp[i+1][k] = max(dp[i+1][k], dp[i][j] + p[i]);
			}
		}
	}
	
	vector<int> ans;
	bool flag = false;
	cin >> m;
	for(int i = 0; i < m; i++){
		int w;
		cin >> w;
		if( dp[n][w] == -1 ){
			flag = true;
		}else{
			ans.push_back(dp[n][w]);
		}
	}
	if( flag ){
		cout << -1 << endl;
	}else{
		for(int i = 0; i < ans.size(); i++){
			cout << ans[i] << endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;

main(){
	int n,m,i,j;
	cin >> n;
	vector<int> dp(400, -1);
	dp[0] = 0;
	while(n--){
		int s,d,p;
		cin >>s>>d>>p;
		for(i=393-s;i>=0;i--){
			if(dp[i]==-1) continue;
			for(j=s;j<=d&&i+j<=393;j++) dp[i+j] = max(dp[i+j], dp[i] + p);
		}
	}
	cin >> n;
	vector<int> ans;
	while(n--){
		cin >> m;
		if(dp[m] == -1){
			cout << "-1" << endl;
			return 0;
		}
		ans.push_back(dp[m]);
	}
	for(i=0;i<ans.size();i++) cout << ans[i] << endl;
	return 0;
}