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++: #define _GLIBCXX_DEBUG
#include "bits/stdc++.h"
//#include <intrin.h>  //AtCoder (gcc) 上ではこれがあると動かない。__popcnt用のincludeファイル。
using namespace std;
using Graph = vector<vector<int>>;

typedef long long ll;
typedef long double ld;


#define int long long
#define rep(i, n) for(long long i = 0; i < (n); i++)
#define sqrt(d) pow((long double) (d), 0.50)

const int INF = 2e9;
const ll large_P = 1e9 + 7;



//繰り返し2乗法
//N^aの、Mで割った余りを求める。
ll my_pow(ll N, ll a, ll M) {
	ll tempo;
	if (a == 0) {
		return 1;
	}
	else {
		if (a % 2 == 0) {
			tempo = my_pow(N, a / 2, M);
			return (tempo * tempo) % M;
		}
		else {
			tempo = my_pow(N, a - 1, M);
			return (tempo * N) % M;
		}
	}
}



//N_C_a を M で割った余り
ll my_combination(ll N, ll a, ll M) {
	ll answer = 1;

	rep(i, a) {
		answer *= (N - i);
		answer %= M;
	}

	rep(i, a) {
		answer *= my_pow(i + 1, M - 2, M);
		answer %= M;
	}

	return answer;
}



ll my_gcd(ll& a, ll& b) {
	if (b == 0) return a;
	ll temp = a % b;
	return my_gcd(b, temp);
}



// long long型 n を、base 進法での、i 桁目の数が入った vector にする。
void ll_to_vector(ll base, ll n, vector<ll>& v) {
	ll tempo = n;

	ll n_digit = floor(log10(n) / log10(base)) + 1;
	v.assign(n_digit, 0);

	//ll n_digit = v.size();

	for (ll i = 0; i < n_digit; i++) {
		v.at(i) = tempo / pow(base, n_digit - 1 - i);
		tempo -= v.at(i) * pow(base, n_digit - 1 - i);
	}
}



int char_to_int(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4;
	case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9;
	default: return 0;
	}
}


//target が素数かどうか判定する。
void prime_judge(vector<bool>& prime_or_not) {

	prime_or_not.at(0) = false;
	prime_or_not.at(1) = false;

	ll n = prime_or_not.size() - 1;
	ll region = sqrt(n) + 1;

	for (ll i = 2; i <= n / 2; i++) {
		prime_or_not.at(2 * i) = false;
	}


	for (ll i2 = 1; i2 < n / 2; i2++) {
		ll i = 2 * i2 + 1;  //ここからは奇数のみ探索
		if (i < region && prime_or_not.at(i)) {
			ll j = i * i;
			while (j < n + 1) {
				prime_or_not.at(j) = false;
				j += 2 * i;
			}
		}
	}
};


const int max_D = 200;
const int max_N = 200;

//派手さの差の絶対値を保持。
//第一変数は、最後に着た服の種類。第二変数は、日付。
int dp[max_N+1][max_D+1];


void solve(vector<int> A, vector<int> B, vector<int> C, vector<int> T) {
	int N = A.size();
	int D = T.size();

	rep(j, D) {
		rep(i, N) {
			if (T.at(j) < A.at(i) || B.at(i) < T.at(j)) dp[i][j] = -INF;  // j 日目に i 番目の服は着られない。
			else if (j != 0) {
				int tempo = dp[0][j - 1] + abs(C.at(0) - C.at(i));
				rep(k, N) {
					if (tempo < dp[k][j - 1] + abs(C.at(k) - C.at(i))) {
						tempo = dp[k][j - 1] + abs(C.at(k) - C.at(i));
					}
				}
				dp[i][j] = tempo;
			}
		}
	}
}



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

	vector<int> T(D);
	rep(i, D) cin >> T.at(i);

	vector<int> A(N), B(N), C(N);
	rep(i, N) cin >> A.at(i) >> B.at(i) >> C.at(i);

	solve(A, B, C, T);

	int res = dp[0][D-1];
	rep(i, N) {
		if (res < dp[i][D-1]) res = dp[i][D-1];
	}

	cout << res << endl;

	//rep(i, N) {
		//rep(j, D) {
			//cout << dp[i][j] << " ";
		//}
		//cout << endl;
	//}

}

C++: #include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int D, N, T[400], A[400], B[400], C[400], X[20][400] = { 0 };
	int F[10][400] = { 0 };
	int f[10][400];
	int ans = 0;
	scanf("%d %d", &D, &N);
	for (int i = 1; i <= D; i++) {
		scanf("%d", &T[i]);
	}
	for (int i = 1; i <= N; i++) {
		scanf("%d %d %d", &A[i], &B[i], &C[i]);
	}
	for (int i = 1; i <= D; i++) {
		X[1][i] = 100;
	}
	for (int i = 1; i <= D; i++) {
		for (int j = 1; j <= N; j++) {
			if (A[j] <= T[i] && B[j] >= T[i] && C[j] > X[0][i]) {
				X[0][i] = C[j];
			}
			if (A[j] <= T[i] && B[j] >= T[i] && C[j] < X[1][i]) {
				X[1][i] = C[j];
			}
		}
		//printf("%d %d %d\n", i, X[0][i], X[1][i]);
	}
	f[0][1] = 0;
	f[1][1] = 1;
	for (int i = 2; i <= D; i++) {
		if (abs(X[0][i] - X[0][i - 1]) > abs(X[0][i] - X[1][i - 1])) {
			F[0][i] = F[0][i - 1] + abs(X[0][i] - X[0][i - 1]);
		}
		else {
			F[0][i] = F[1][i - 1] + abs(X[0][i] - X[1][i - 1]);
		}
		if (abs(X[1][i] - X[0][i - 1]) > abs(X[1][i] - X[1][i - 1])) {
			F[1][i] = F[0][i - 1] + abs(X[1][i] - X[0][i - 1]);
		}
		else {
			F[1][i] = F[1][i - 1] + abs(X[1][i] - X[1][i - 1]);
		}
	}

	printf("%d\n",max(F[0][D],F[1][D]));
	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {

	int min = all[0];
	for (int i = 1; i < n; ++i) {
		if (all[0] > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[0];
	for (int i = 1; i<n; ++i) {
		if (all[0]<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}

C++: #include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;
struct cloth{
    int low;
    int high;
    int c;
};
int main(void){

    int D,N;
    cin>>D>>N;
    int t[300];
    cloth d[300];
    for(int i = 1;i <= D;i++){
        cin>>t[i];
    }
    for(int i = 1;i <= N;i++){
        cin>>d[i].low>>d[i].high>>d[i].c;
    }

    int dp[300][300];
    for(int i = 1;i <= D;i++){
        for(int j = 1;j <= N;j++){
            dp[i][j] = -1;
        }
    }
    for(int j = 1;j <= N;j++){
        if(d[j].low <= t[1] && t[1] <= d[j].high)dp[1][j] = 0;
    }
    for(int i = 2;i <= D;i++){
        for(int j = 1;j <= N;j++){
            if(dp[i-1][j]!=-1){
                for(int k = 1;k <= N;k++){
                    if(d[k].low <= t[i] && t[i] <= d[k].high){
                        dp[i][k] = max(dp[i][k],dp[i-1][j] + abs(d[j].c - d[k].c));
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int j = 1;j <= N;j++){
        //cout<<j<<"の服";
        for(int i = 1;i <= D;i++){
            //cout<<i<<"日目: "<<dp[i][j]<<" ";
        }
        //cout<<endl;
    }
    for(int j = 1;j <= N;j++){
        ans = max(ans,dp[D][j]);
    }
    cout<<ans<<endl;

    return 0;
}