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 <queue>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef long long int ll;

int n;

int dpdiv[400]={0};
int dppnt[400]={0};

void set(int s, int d, int p){
	if(393 < s) return;
	if(dppnt[s] < p){
		if(dpdiv[s] > d) set(s + d, dpdiv[s] - d, dppnt[s]);
		dppnt[s] = p;
		dpdiv[s] = d;
	}else{
		if(dpdiv[s] < d) set(s + dpdiv[s], d - dpdiv[s], p);
	}
}

main(){
	int i,j;
	cin >> n;
	while(n--){
		int s,d,p;
		cin >>s>>d>>p;
		for(i=393;i;i--) if(dppnt[i]) set(i+s, dpdiv[i] + d-s, p+dppnt[i]);
		set(s, d-s, p);
	}
	for(i=0;i<394;i++){
		for(j=1;j<=dpdiv[i];j++){
			set(i+j, dpdiv[i]-j, dppnt[i]);
		}
	}
	cin >> n;
	vector<int> ans;
	while(n--){
		int m;
		cin >> m;
		if(!dppnt[m]){
			cout << "-1" << endl;
			return 0;
		}
		ans.push_back(dppnt[m]);
	}
	for(i=0;i<ans.size();i++) cout << ans[i] << endl;
	return 0;
}

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

int main(){
	int n;
	cin>>n;
	int po[394]={0};
	for(int i=0,a,b,c;i<n;i++){
		cin>>a>>b>>c;
		for(int j=a;j<=b;j++)po[j]=max(po[j],c);
	}
	
	for(int i=1;i<=393;i++){
		for(int j=1;j<=i;j++){
			if(po[i]>0&&po[j]>0&&i+j<=393){
				po[i+j]=max(po[i+j],po[i]+po[j]);
			}
		}
	}
	
	int m;
	cin>>m;
	int data[394];
	data[0]=1;
	for(int i=1;i<=m;i++){
		cin>>data[i];
		if(po[data[i]]==0)data[0]=0;
	}
	
	if(!data[0])cout<<-1<<endl;
	else{
		for(int i=1;i<=m;i++){
			cout<<po[data[i]]<<endl;
		}
	}
	/*
	cout<<"#"<<endl;
	for(int i=1;i<=10;i++){
		cout<<po[i]<<endl;
	}
	*/
	return 0;
}

C++: 1;2c#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;
int n, w, dp[1000][1000];
vector<int>p, len;

int rec(int i, int j){
  if(dp[i][j] >= 0) return dp[i][j];
  int res;
  if(i == p.size() || j == 0) res = 0;
  else if(j < len[i]) res = rec(i + 1, j);
  else  res = max(rec(i + 1, j), rec(i + 1, j - len[i]) + p[i]);

  return dp[i][j] = res;
}

int main(){
  int n, m, s, l, point;
  memset(dp, -1, sizeof(dp));
  scanf("%d", &n);
  rep(i, n){
    scanf("%d%d%d", &s, &l, &point);
    for(int j = s; j <= l; j++){ len.push_back(j); p.push_back(point);}
  }
  scanf("%d", &m);
  vector<int>res;
  rep(i, m){
    scanf("%d", &w);
    res.push_back(rec(0, w));
  }
  bool f = true;
  rep(i, res.size()) if(res[i] == 0) f = false;
  if(f) rep(i, res.size()) printf("%d\n", res[i]);
  else puts("-1");
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;

int s[500],l[500],p[500];
int w[500];
int dp[500][500];

int main() {
  int n;
  while(cin >> n) {
    REP(i,n) {
      cin >> s[i] >> l[i] >> p[i];
    }
    memset(dp,-1,sizeof(dp));
    dp[0][0] = 0;
    REP(i,n) {
      REP(j,400) dp[i+1][j] = dp[i][j];
      for (int j=s[i]; j<=l[i]; ++j) {
        REP(k,400-j) {
          if (dp[i][k] == -1) continue;
          chmax(dp[i+1][j+k], dp[i][k] + p[i]);
        }
      }
    }
//    REP(i,10) cout << dp[n][i] << " " ; cout << endl;
    int m; cin >> m;
    vector<int> ans;
    bool flag = 0;
    REP(i,m) {
      int w; cin >> w;
      if (dp[n][w] == -1) flag = 1;
      ans.push_back(dp[n][w]);
    }
    if (flag) cout << -1 << endl;
    else {
      FOR(it, ans) cout << *it << endl;
    }
  }
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;

#define REP(i, n) for (int (i) = 0; (i) < (int)(n); ++(i))
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) {
    return s << '<' << P.first << ", " << P.second << '>';
}
template<class T> ostream& operator << (ostream &s, vector<T> P) {
    for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl;
}
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) {
    EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl;
}
template<class T> void print(T a, T b) { 
    for (T i = a; i != b; ++i) { if (i != a) { cout << " "; } cout << *i; } cout << endl; 
}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }




const int MAX = 400;
int dp[MAX];
int s[MAX], l[MAX], p[MAX], w[MAX];

int n, m;

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> s[i] >> l[i] >> p[i];
    cin >> m;
    for (int i = 0; i < m; ++i) cin >> w[i];
        
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < MAX; ++j) {
            for (int k = s[i]; k <= l[i]; ++k) {
                if (j + k < MAX) chmax(dp[j+k], dp[j] + p[i]);
            }
        }
    }

    vint res; 
    bool valid = true;
    for (int i = 0; i < m; ++i) {
        res.push_back(dp[w[i]]);
        if (dp[w[i]] == -1) valid = false;
    }
    if (valid) for (int i = 0; i < m; ++i) cout << res[i] << endl;
    else puts("-1");
    
    return 0;
}