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

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