Go: ///
// File:  0579.go
// Author: ymiyamoto
//
// Created on Mon Mar 19 18:46:50 2018
//
package main

import (
	"fmt"
)

var D, N int

type cloth struct {
	A, B, C int
}

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func main() {
	fmt.Scan(&D, &N)
	thermals := make([]int, D+1)

	for i := 1; i <= D; i++ {
		fmt.Scan(&thermals[i])
	}

	clothes := make([]cloth, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&clothes[i].A, &clothes[i].B, &clothes[i].C)
	}

	dp := make([][]int, D+1)
	for i := range dp {
		dp[i] = make([]int, N)
	}

	for i := 1; i < D; i++ {
		for j := 0; j < N; j++ {
			for k := 0; k < N; k++ {
				t1 := thermals[i]
				t2 := thermals[i+1]
				if clothes[j].A <= t2 && t2 <= clothes[j].B && clothes[k].A <= t1 && t1 <= clothes[k].B {
					dp[i+1][j] = max(dp[i+1][j], dp[i][k]+abs(clothes[j].C-clothes[k].C))
				}
			}
		}
	}

	val := 0
	for _, v := range dp[D] {
		val = max(val, v)
	}

	fmt.Println(val)
}

C++: #include <algorithm>
#include <iostream>
#include <array>
#include <vector>
#include <cmath>
using namespace std;
 
const int D=200;
const int N=200;
 
struct Clothes{
    int max_t;
    int min_t;
    int c;
};
 
int main(){
    int d,n;
    array<array<int,N>,D> dp;
    vector<Clothes> clt;
    vector<int> tmp;
 
    cin>>d>>n;
 
    tmp.resize(d);
 
    for( auto &t : tmp ) cin>>t;
 
    clt.resize(n);
 
    for( auto &t : clt ) cin>>t.min_t>>t.max_t>>t.c;
 
    for( auto &x : dp ){
        for( auto &y : x ){
            y=0;
        }
    }
 
    for( int i=1 ; i<d ; i++ ){
        for( int j=0 ; j<n ; j++ ){
            if( tmp[i] >= clt[j].min_t && tmp[i] <= clt[j].max_t ){
                for( int k=0 ; k<n ; k++){
                    if( tmp[i-1] >= clt[k].min_t && tmp[i-1] <= clt[k].max_t ){
                        dp[i][j]=max(dp[i][j],abs(clt[j].c-clt[k].c)+dp[i-1][k]);
                    }
                }
            }
        }
    }
 
    cout << accumulate(dp[d-1].begin(),dp[d-1].begin()+n,0,[](int a,int b){ return ( a>b ? a : b ); }) <<endl;
 
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

struct cl{
	int min;
	int max;
	int f;
};

int DP[500][200]; // DP[i][j] : ?????\?????????????´?????????????????????????????????§???

int main(){
	
	int D, N, ans;
	
	cin >> D >> N;
	
	int d[500];
	
	cl cloth[500];
	
	for(int i = 1; i <= D; i++){
		cin >> d[i];
	}
	
	for(int i = 0; i < N; i++){
		cin >> cloth[i].min >> cloth[i].max >> cloth[i].f;
	}
	
	for(int i = 0; i <= D; i++){
		for(int j = 0; j <= 100; j++){
			DP[i][j] = -(1 << 30);
		}
	}
	
	DP[0][0] = 0;
	
	for(int j = 0; j < N; j++){
		if(cloth[j].min <= d[1] && cloth[j].max >= d[1]){
			DP[1][cloth[j].f] = 0;
		}
	}
	
	for(int i = 2; i <= D; i++){
		for(int j = 0; j < N; j++){
			if(cloth[j].min <= d[i] && cloth[j].max >= d[i]){
				for(int k = 0; k <= 100; k++){
					if(cloth[j].f - k >= 0){
						DP[i][cloth[j].f] = max(DP[i][cloth[j].f], DP[i - 1][cloth[j].f - k] + k);
					}
					if(cloth[j].f + k <= 100){
						DP[i][cloth[j].f] = max(DP[i][cloth[j].f], DP[i - 1][cloth[j].f + k] + k);
					}
				}
			}
		}
	}
	
	ans = -(1 << 30);
	
	/*
	for(int i = 1; i <= D; i++){
		for(int j = 0; j <= 100; j++){
			if(DP[i][j] < 0){
				printf("0 ");
			}else{
				printf("%d ", DP[i][j]);
			}
		}
		printf("\n");
	}
	*/
	for(int j = 0; j <= 100; j++){
		if(DP[D][j] > ans){
			ans = DP[D][j];
		}
	}
	
	printf("%d\n", ans);
	
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)
#define rrep(i,x) for(int i=(int)x-1;i>=0;--i)
#define rrep1(i,x) for(int i=(int)x;i>=1;--i)

using ll=long long;
using ld=long double;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int, int>;
using vpii=vector<pii>;

template<class T,class U>ostream&operator<<(ostream&os,const pair<T,U>p){os<<"("<<p.fst<<", "<<p.scd<<")";return os;}
template<class T>ostream&operator<<(ostream&os,const vector<T>v){rep(i,v.size()){if(i)os<<", ";os<<v[i];}return os;}
template<typename T>T&max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T&min(T&a,T&b){if(a < b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a < b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a > b){a=b;return true;}return false;}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

int D, N;

int T[205], A[205], B[205], C[205];

int dp[205][205];

signed main()
{
    cin >> D >> N;

    rep(i, D) {
        cin >> T[i];
    }

    rep(i, N) {
        cin >> A[i] >> B[i] >> C[i];
    }

    for (int i = 1; i < D; ++i) {
        rep(j, N) {
            if (A[j] <= T[i] && T[i] <= B[j]) {
                rep(k, N) {
                    if (A[k] <= T[i - 1] && T[i - 1] <= B[k]) {
                        chmax(dp[i][j], dp[i - 1][k] + abs(C[j] - C[k]));
                    }
                }
            }
        }
    }

    int ma = 0;

    rep(i, N) {
        chmax(ma, dp[D - 1][i]);
    }

    cout << ma << endl;
}

C++: #include<stdio.h>
#include<math.h>
//abs .. 絶対値を返す関数　がある。


int n,d;
int dp[205][205];
//dp[a][b] .. a日目にb番目の服を着るような状態のうち、得られる得点の最大値。着れなければ十分小さい数

int hukup[205];
int hukdw[205];
int hukha[205];
int ond[205];


int inf = -1000000;


int max(int p,int q){
	if(p<q)return q;
	return p;
}

int main(void){
	memset(dp,-1,sizeof(dp));
	scanf("%d%d",&d,&n);
	for(int i=1;i<=d;i++){
		scanf("%d",&ond[i]);
	}
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&hukdw[i],&hukup[i],&hukha[i]);
	}
	//入力の受付ここまで
	
	for(int j=1;j<=n;j++){
		if(hukdw[j]<=ond[1] && ond[1]<=hukup[j]){
			dp[1][j] = 0;
		}
		else{
			dp[1][j] = inf;
		}
	}
	//初日を初期化。
	
	for(int i=2;i<=d;i++){
		for(int j=1;j<=n;j++){
			//i日目に、j番目の服を着るときの最大値
			if(hukdw[j]<=ond[i] && ond[i]<=hukup[j]){
				//着られる
				dp[i][j]=0;
				for(int k=1;k<=n;k++){
					//現在の最大値と、(前日にkを着て、今日jを着るようなパターン) のうち、大きい方をとっていく
					dp[i][j] = max(dp[i][j] , dp[i-1][k] + abs(hukha[j]-hukuha[k]) );
				}
			}
			else{
				//着られない
				dp[i][j] = inf;
			}
		}
	}
	
	int ans = 0;
	for(int j=1;j<=n;j++){
		//最終日のうちの、最大値
		ans = max(ans,dp[d][j]);
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

int D, N, T[212], A[212], B[212], C[212];
int dp[212][212], ans;

int main()
{
	in >> D >> N;
	rep(i, D) { in >> T[i]; }
	rep(i, N) { in >> A[i] >> B[i] >> C[i]; }

	rep(i, D)
	{
		rep(j, N)
		{
			if (A[j] <= T[i] && T[i] <= B[j])
			{
				rep(k, N)
				{
					if (A[k] <= T[i + 1] && T[i + 1] <= B[k])
					{
						dp[i + 1][k] = std::max(dp[i + 1][k], dp[i][j] + abs(C[j] - C[k]));
					}
				}
			}
		}
	}
	rep(i, N)
	{
		ans = std::max(ans, dp[D - 1][i]);
	}

	out << ans << std::endl;
	return 0;
}