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++: // aribon2-3-3_a
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

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

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))
#define ALL(x) x.begin(), x.end()

#define INF ((1 << 29)-1)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
template<class T> void print(const T& x){cout << x << endl;}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

int dp[394];

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt");
    cin.rdbuf(in.rdbuf());
#endif

    int N;
    cin >> N;

    vector<P> sl(N);
    vector<int> p(N);
    REP(i, N) cin >> sl[i].first >> sl[i].second >> p[i];

    int M;
    cin >> M;
    vector<int> Ws(M);
    REP(i, M) cin >> Ws[i];

    REP(j, 394) dp[j] = -1;
    dp[0] = 0;
    REP(i, N){
        int s = sl[i].first;
        int l = sl[i].second;
        REP(j, 394){
            if (dp[j] == -1) continue;
            REPN(k, s, l+1){
                if (j + k > 393) continue;
                dp[j+k] = max(dp[j+k], dp[j] + p[i]);
            }
        }
    }

    REP(i, M){
        if (dp[Ws[i]] == -1) {
            print(-1);
            return 0;
        }
    }

    REP(i, M) {
        print(dp[Ws[i]]);
    }

    return 0;
}

C++: #include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<limits.h>
#include<utility>
#include<bits/stdc++.h>
#include<vector>
#include<tuple>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define ALL(A) A.begin(),A.end()


int main() {
	int n;
	cin >> n;
	int a[n][3];
	rep(i, n) {
		cin >> a[i][0] >> a[i][1] >> a[i][2];
	}
	int m;
	cin >> m;
	int p[m];
	int b;
	vector<int> point(394,INT_MIN);
	rep(i, m) {
		cin >> b;
		p[i] = b;
		if (point[b] == INT_MIN) {
			vector<int> c(b + 1, INT_MIN);

			c[0] = 0;

			rep(j, b) {
				rep(k, n) {
					for (int l = a[k][0]; l <= a[k][1]; l++) {
						if (b >= l + j) {
							if (c[l + j] < c[j] + a[k][2]) {
								c[l + j] = c[j] + a[k][2];
							}
						}
					}
				}
			}
			if (c[b] == INT_MIN) {
				cout << -1 << endl;
				return 0;
			}
			//p[i] = c[b];
			point[b] = c[b];
		}
	}
	rep(i, m) {
		cout << point[p[i]] << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define X first
#define Y second
using namespace std;
const int MAX = 394; 
int dp[394];

void DP(int amount, int v){
	for(int i = 1; i < MAX; i++){
		if(amount <= i && dp[i-amount]) dp[i] = dp[i-amount]+v>dp[i] ? dp[i-amount]+v : dp[i];
	}
}

int main(){
	int n;
	cin >> n;
	fill(dp, dp+MAX, 0);
	while(n--){
		int up, down, v;
		cin >> down >> up >> v;
		while(down <= MAX){
			if(up>=MAX) up = MAX-1;
			for(int i = down; i <= up; i++) dp[i] = dp[i]<v?v:dp[i];
			for(int i = 0; i < 2; i++){
				if(i) DP(up, v);
				else DP(down, v);
			}
			up += up;
			down += down;
			v += v;
		}
	}
	int m;
	cin >> m;
	vector<int> ans;
	while(m--){
		int input;
		cin >> input;
		if(dp[input]) ans.push_back(dp[input]);
		else{
			cout << "-1" << endl;
			return 0;
		}
	}
	for(int i = 0; i < ans.size(); i++){
		cout << ans[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
using ll = long long;
using namespace std;

int w[401];
vector<tuple<int, int, int>> v;
int dp[401]; //indexの番号 重さ 最大コストをもつ
int const MOD = 1e9 + 7;
int main(void)
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        int r, l, p;
        cin >> r >> l >> p;
        v.push_back(make_tuple(r, l, p));
    }

    int m;
    cin >> m;
    for (int i = 0; i < m; ++i)
        cin >> w[i];

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= 393; ++j)
        {
            for (int k = get<0>(v[i]); k <= get<1>(v[i]); ++k)
            {
                if (j - k >= 0)
                {
                    dp[j] = max(dp[j], dp[j - k] + get<2>(v[i]));
                }
                else
                {
                    dp[j] = dp[j];
                }
            }
        }
    }

    bool ok = true;
    vector<int> ans;
    for (int i = 0; i < m; ++i)
    {
        if (dp[w[i]] == 0)
            ok = false;
        ans.push_back(dp[w[i]]);
    }
    if (!ok)
        cout << -1 << endl;
    else
    {
        for (auto e : ans)
            cout << e << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<set>
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class Phrase {
    public:
    int s,l,p;
    Phrase(int s,int l,int p) {

        this->s = s;
        this->l = l;
        this->p = p;
    }
    Phrase() {
        s=l=p=0;
    }

};

int n;
Phrase phra[400];
int m;
int w[400];
int dp[400];

int main() {

    cin>>n;
    rep(i,n) {
        int s,l,p;
        cin>>s>>l>>p;
        phra[i] = Phrase(s,l,p);
    }
    cin>>m;
    rep(i,m) cin>>w[i];

    rep(i,n) {
        for(int j=phra[i].s;j<=phra[i].l;j++) {
            for(int k=0;k<400;k+=j) {
                dp[k+j] = max(dp[k+j],dp[k]+phra[i].p);
            }
        }
    }

    multiset<int> ans;
    
    rep(i,m) ans.insert(dp[w[i]]);    
    if(ans.find(0) != ans.end()) {
        cout<<"-1"<<endl;
        return 0;
    }

    multiset<int>::iterator it = ans.begin();
    while(it != ans.end()) {
        cout<<(*it)<<endl;
        it++;
    }




    return 0;
}