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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int D,N,T[201],fuku[201][3],sa,dp[201][201],ans;
bool wear[201][201];
int main(){
    cin>>D>>N;
    for(int i=0;i<D;i++){
        cin>>T[i];
    }
    for(int i=0;i<N;i++){
        cin>>fuku[i][0]>>fuku[i][1]>>fuku[i][2];
    }
    for(int i=0;i<D;i++){
        for(int j=0;j<N;j++){
            if(fuku[j][0]<=T[i]&&T[i]<=fuku[j][1])wear[i][j]=true;
        }
    }
    for(int i=1;i<D;i++){
        for(int j=0;j<N;j++){
            for(int k=0;k<N;k++){
                if(wear[i][j]&&wear[i-1][k]){
                    sa=fuku[j][2]-fuku[k][2];
                    if(sa<0)sa=sa*-1;
                    dp[i][j]=max(dp[i][j],dp[i-1][k]+sa);
                }
            }
        }
    }
    for(int i=0;i<N;i++){
        ans=max(ans,dp[D-1][i]);
    }
    cout<<ans<<endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

#define MAX_D 210
#define MAX_N 210

int D, N;
int T[201];
int A[201], B[MAX_N], C[MAX_N];
bool suit[MAX_D][MAX_N];
int dp[MAX_D][MAX_N];

int main() {
    int i, j, k;
    
    scanf("%d%d", &D, &N);
    for (i = 0; i < D; ++i) {
        scanf("%d", &T[i]);
    }
    for (j = 0; j < N; ++j) {
        scanf("%d%d%d", &A[j], &B[j], &C[j]);
    }
    
    for (i = 0; i < D; ++i) for (j = 0; j < N; ++j) {
        suit[i][j] = (A[j] <= T[i] && T[i] <= B[j]);
    }
    
    for (i = 0; i < D; ++i) for (j = 0; j < N; ++j) {
        //	-1 : impossible
        dp[i][j] = -1;
    }
    for (j = 0; j < N; ++j) if (suit[0][j]) {
        dp[0][j] = 0;
    }
    for (i = 0; i < D - 1; ++i) for (j = 0; j < N; ++j) if (dp[i][j] >= 0) {
        for (k = 0; k < N; ++k) if (suit[i + 1][k]) {
            if (dp[i + 1][k] < dp[i][j] + abs(C[j] - C[k])) {
                dp[i + 1][k] = dp[i][j] + abs(C[j] - C[k]);
            }
        }
    }
    
    int ans = -1;
    for (j = 0; j < N; ++j) {
        if (ans < dp[D - 1][j]) {
            ans = dp[D - 1][j];
        }
    }
    printf("%d\n", ans);
    
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[250][150],T[250],x[250][3],D,N,MAX;
memset(dp,-1000000,sizeof(dp));
main(){ 
    cin>>D>>N;
    for(int i=1;i<=D;i++){cin>>T[i];}
    for(int i=1;i<=N;i++){
        cin>>x[i][0]>>x[i][1]>>x[i][2];
        if(T[1]>=x[i][0] && T[1]<=x[i][1]){dp[1][x[i][2]]=0;}
    }
    for(int i=2;i<=D;i++){
        for(int j=1;j<=N;j++){
            if(T[i]>=x[j][0] && T[i]<=x[j][1]){
                for(int k=0;k<=100;k++){
                    dp[i][x[j][2]]=max(dp[i][x[j][2]],dp[i-1][k]+abs(x[j][2]-k));
                }
            }
        }
    }
    for(int i=0;i<=100;i++){MAX=max(MAX,dp[D][i]);}
    cout<<MAX<<endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define rep(i, a, n) for(int i = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int i = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int i = (int)(a);(x);++(i))
#define repr(i, a, n) for(int i = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int i = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x)/gcd(x,y)*(y))
#define isin(x, l, r) ((l) <= (x) and (x) < (r))
#define fcout(n) cout << fixed << setprecision(n)
#define rcout(n) cout << right << setw(n)
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define PI (acosl(-1))
#define EPS (1e-10)
#define INF LLONG_MAX
#define INF32 INT32_MAX
#define MOD 1000000007LL
using namespace std;
using VI = vector<int>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<PII>;
using VTIII = vector<TIII>;
using Complex = complex<double>;
template<typename T, template<typename> typename C = less> using PQ = priority_queue<T, vector<T>, C<T>>;
template<typename T> inline bool chmax(T& a, const T& b, bool t = false){ if(a<b or (t and a == b)){ a=b; return true; } return false; }
template<typename T> inline bool chmin(T& a, const T& b, bool t = false){ if(a>b or (t and a == b)){ a=b; return true; } return false; }


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


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

    rep(i, 0, N) if(!isin(T[0], A[i], B[i]+1)) dp[1][i] = -INF;
    rep(i, 1, D) rep(j, 0, N) rep(k, 0, N) if(isin(T[i], A[j], B[j]+1)) chmax(dp[i+1][j], dp[i][k]+abs(C[j]-C[k]));

    cout << *max_element(dp[D], dp[D]+N) << endl;

    return 0;
}