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 <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define MAX 393
using namespace std;

struct state{
	state(int a, int b, int c):down(a), up(b), point(c){}
	int down;
	int up;
	int point;
};

int main(){
	int n = 0;
	cin >> n;
	vector<int> table(MAX+1, -1);
	state combi(0, 0, 0);
	for(int i = 0; i < n; i++){
		state input(0, 0, 0);
		cin >> input.down >> input.up >> input.point;
		state incopy = input;
		while(incopy.down < MAX){
			if(incopy.up > MAX) incopy.up = MAX;
			for(int j = incopy.down; j <= incopy.up; j++){
				if(table[j]<incopy.point)table[j] = incopy.point; 
			}
			state ccopy = combi;
			while(ccopy.down&&incopy.down+ccopy.down < MAX){
				if(incopy.up+ccopy.up > MAX) ccopy.up = MAX-incopy.up;
				for(int j = incopy.down+ccopy.down; j <= incopy.up+ccopy.up; j++){
					if(table[j]<incopy.point+ccopy.point)table[j] = incopy.point+ccopy.point; 
				}
				ccopy.down += combi.down;
				ccopy.up += combi.up;
				ccopy.point += combi.point;
			}
			incopy.down += input.down;
			incopy.up += input.up;
			incopy.point += input.point;
		}
		combi.down += input.down;
		combi.up += input.up;
		combi.point += input.point;
	}

	queue<int> q;
	int m = 0;
	cin >> m;
	for(int i = 0; i < m; i++){
		int tmp = 0;
		cin >> tmp;
		if(table[tmp]!=-1) q.push(table[tmp]);
		else{
			cout << "-1" << endl;
			return 0;
		}
	}
	while(!q.empty()){
		cout << q.front() << endl;
		q.pop();
	}

	return 0;
}

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

using namespace std;

int main(){
  int n;
  cin>> n;
  int d[400],u[400],s[400];
  for(int i=0;i<n;i++) cin>>d[i]>>u[i]>>s[i];
  
  int dp[400]={};
  memset(dp,-1,sizeof(dp));
  dp[0]=0;

  for(int i=0;i<n;i++)
    for(int k=u[i];k>=d[i];k--)
      for(int j=399;j>=0;j--)
	if(j+k<400 && dp[j]!=-1) dp[j+k] = max(dp[j+k],dp[j]+s[i]);
      
  int q,flg=0,m[400];
  cin>> q;
  for(int i=0;i<q;i++){
    cin >> m[i];
    if(dp[m[i]] == -1) flg= 1;
  }
  if(flg) cout << -1<<endl;
  else 
    for(int i=0;i<q;i++) cout << dp[m[i]]<<endl;
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
int dp[3939];
 
int main()
{
    int n;
     
    scanf("%d", &n);
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    for (int i = 0; i < n; i++){
        int low, up, val;
        scanf("%d %d %d", &low, &up, &val);
         
        for (int d = low; d <= up; d++)
            for (int v = 0; v + d <= 393; v++)
                if (~dp[v]) dp[v + d] = max(dp[v] + val, dp[v + d]);
    }
     
    int m;
    scanf("%d", &m);
     
    int ans[3939];
    for (int i = 0; i < m; i++){
        int s;
        scanf("%d", &s);
        if (~dp[s]) ans[i] = dp[s];
        else return (!printf("-1\n"));
    }
     
    for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
     
    return (0);
}

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

int score;
int n, m;
int w;
int s[400], l[400], p[400];
void dp(int num, int melodyLength){
	if(melodyLength == w){
		score = max(score, num);
	}

	for(int i=0; i < n; i++){
		for(int j=s[i]; j <= l[i]; j++){
			if(melodyLength + j <= w){
				dp(num + p[i], melodyLength + j);
			}
		}
	}
}

int main(){
	cin >> n;
	for(int i=0; i < n; i++){
		cin >> s[i] >> l[i] >> p[i];
	}
	cin >> m;
	for(int j=0; j < m; j++){
		score = 0;
		cin >> w;
		dp(score, 0);

		cout << score << endl;
	}


}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

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

int dp[400][401];

int main() {
    cin >> n;
    rep(i, n) {
        cin >> s[i] >> l[i] >> p[i];
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= 400; ++j) {
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
            for (int k=s[i]; k<=l[i]; ++k) {
                if (j >= k) {
                    dp[i+1][j]=max({dp[i][j], dp[i+1][j], dp[i+1][j-k]+p[i]});
                }
                else dp[i+1][j] = max(dp[i][j], dp[i+1][j]);
            }
        }
    }
    cin >> m;
    bool ok = true;
    rep(i, m) {
        cin >> w[i];
        if (dp[n][w[i]] == 0) {
            ok = false;
        }
    }
    if (!ok) {
        cout << -1 << endl;
        return 0;
    }
    rep(i, m) {
        cout << dp[n][w[i]] << endl;
    }
    return 0;
}