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 := 0; 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<bits/stdc++.h>
#define LL long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)

using namespace std;

static const LL MAX_N = 1e+5;
static const LL INF = 1 << 25;

struct clothes{
    LL A[100],B[100],C[150];
};

int N,D;
LL res=-INF;
LL Day[300];
LL mem[300][100000];
clothes T;
int i;

LL cal(LL day,LL p,LL pp,LL sum){
    //cout<<res<<" "<<day<<endl;
    if(res<sum+abs(pp-p)&&day==N+1){
        mem[day][sum+abs(pp-p)]=res=sum+abs(pp-p);
    }
    else if(mem[day][sum+abs(pp-p)]!=0){
           // cout<<"hoge"<<endl;
        return mem[day][sum+abs(pp-p)];
    }
    else{
        if(day==N+1)return 0;
        else if(day==0){
            FOR(i,1,D)
            if(Day[1]>=T.A[i]&&Day[1]<=T.B[i])cal(day+1,T.C[i],p,sum);
        }
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]){
                if(day>2)cal(day+1,T.C[i],p,sum+abs(pp-p));
                else cal(day+1,T.C[i],p,sum);
            }
        }
    }
    return res;
}


int main(){
    cin>>N>>D;
    FOR(i,1,N)cin>>Day[i];
    FOR(i,1,D)
        cin>>T.A[i]>>T.B[i]>>T.C[i];
    cout<<cal(0,0,0,0)<<endl;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 210

signed main() {
	//??????dp A<=??????<=B?????¨??? dp[i??\???][j????????????] ?????????
	int D, N; cin >> D >> N;
	int T[MAX] = {};
	rep(i, 0, D)cin >> T[i];
	int A[MAX], B[MAX], C[MAX];
	rep(j, 0, N)cin >> A[j] >> B[j] >> C[j];
	int dp[MAX][MAX] = {};
	fill(dp[0], dp[MAX], -1);
	rep(j, 0, N) {
		if (T[0] < A[j] || B[j] < T[0])continue;
		dp[1][j] = 0;
	}
	rep(i, 1, D) {
		rep(j, 0, N) {
			if (T[i] < A[j] || B[j] < T[i])continue;
			rep(k, 0, N) {
				if (dp[i][k] == -1)continue;
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + abs(C[j] - C[k]));
			}
		}
	}
	cout << *max_element(dp[D], dp[D] + MAX) << endl;;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int D,N;
int T[200];
int A[200],B[200],C[200];

int dp[210][210];

signed main(){
    cin>>D>>N;
    rep(i,D)cin>>T[i];
    rep(i,N)cin>>A[i]>>B[i]>>C[i];

    fill_n(*dp,210*210,-1001001001001001001ll);
    rep(i,N){
        if(A[i]<=T[0]&&T[0]<=B[i])dp[1][i]=0;
    }

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

    int ma=-1001001001001001001ll;
    rep(i,N)chmax(ma,dp[D][i]);
    cout<<ma<<endl;
    return 0;
}

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

 int master(int data){
 if(data>=0)
	 return data;
 return -data;
 }
int main(){
    int D,N,T[201],A[201][3];
    int dp[201][101];
    cin >> N >> D ;
    for(int i=0;i<N;i++){
        cin >>T[i];
    }
    for(int i=0;i<D;i++){
        for(int j=0;j<3;j++){
            cin >>A[i][j];
        }
    }
    for(int i=0;i<N;i++){
        for(int j=0;j<200;j++){
            dp[i][j]=-9999;
        }
    }
    for(int l=0;l<D;l++){
        if(T[0]>A[l][0] && T[0]<A[l][1] ){
            dp[ 0 ][ A[l][2] ]=0;
             
 
        }
    }
    for(int i=1;i<N;i++){
        for(int j=0;j<D;j++){
            if(T[i]>=A[j][0] && T[i]<=A[j][1] ){
                for(int l=0;l<=100;l++){
                    if(dp[i-1 ][ l ]>=0)
                        dp[ i ][ A[j][2] ]=max(dp[ i ][ A[j][2] ],dp[ i-1 ][ l ]+master(l-A[j][2]));//テッツセツεッツスツ、テッツセツづッツスツサテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツセテッツセツεッツスツ」テッツセツ?テッツスツァテッツセツεッツスツ」テッツセツ?テッツスツョテッツセツεッツスツヲテッツセツづヲツ??テッツスツゥテッツセツづッツスツォテッツセツづ、ツスツ堙ッツスツ」テッツセツ??テッツスツ、テッツセツづッツスツサテッツセツづァツ板佚ッツスツ・テッツセツづ・ツエツ「?
                     
 
                }
            }
        }
    }
    int answer=0;
    for(int j=0;j<101;j++)
    {
		
        answer=max(answer,dp[ N-1 ][j]);
    }
    cout << answer << endl;
    return 0;
}

C++: #include <iostream>
#include <limits>
#include <cmath>

constexpr int MAX = 200;
constexpr int MAX_C = 100;

int D, N, T[MAX], A[MAX], B[MAX], C[MAX];
int memo[MAX_C + 1][MAX + 1];

int solve(int tmp, int n = 1);

int main(){
	std::ios::sync_with_stdio(false);
	
	memset(memo, -1, sizeof(memo));

	std::cin >> D >> N;

	for(int i = 0; i < D; ++i){
		std::cin >> T[i];
	}

	for(int i = 0; i < N; ++i){
		std::cin >> A[i] >> B[i] >> C[i];
	}

	int ans = 0;
	for(int i = 0; i < N; ++i){
		if(A[i] <= T[0] && T[0] <= B[i]){
			ans = std::max(ans, solve(C[i]));
		}
	}

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

int solve(int tmp, int n){
	if(n == D){
		return 0;
	}
	if(memo[tmp][n] != -1){
		return memo[tmp][n];
	}

	int res = 0;
	for(int i = 0; i < N; ++i){
		if(A[i] <= T[n] && T[n] <= B[i]){
			res = std::max(res, solve(C[i], n + 1) + abs(tmp - C[i]));
		}
	}
	//std::cout << "memo[" << tmp << "][" << n << "] = " << res << std::endl;
	return memo[tmp][n] = res;
}