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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

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

	vi T(D), A(N), B(N), C(N);
	REP(i, D) {
		cin >> T[i];
	}
	REP(i, N) {
		cin >> A[i] >> B[i] >> C[i];
	}

	vvi dp(D+1, vi(N));
	vvi ok(D+1, vi(N));
	FOR(i, 1, D) {
		REP(j, N) {
			if(A[j] <= T[i-1] && T[i-1] <= B[j]) {
				ok[i][j] = 1;	
			}
		}
	}

	FOR(i, 2, D) {
		REP(j, N) {
			REP(k, N) {
				if(ok[i-1][j] && ok[i][k]) {
					dp[i][k] = max(dp[i][k], dp[i-1][j]+abs(C[j]-C[k]));
				}
			}
		}
	}

	int ans = 0;
	REP(i, N) {
		ans = max(ans, dp[D][i]);
	}
	cout << ans << endl;
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
	const int MAX_N = 200, MAX_D = 200, MAX_T = 60;
	int D, N;
	int T[MAX_D], A[MAX_N], B[MAX_N], C[MAX_N];
	int MAX[MAX_T + 1], MIN[MAX_T + 1];
	int dp[2][MAX_D];//dp[0][i] : choose MAX
	int num1, num2;
	bool update;
	
	scanf("%d%d", &D, &N);
	for(int i = 0; i < D; i++) scanf("%d", &T[i]);
	for(int i = 0; i < N; i++) scanf("%d%d%d", &A[i], &B[i], &C[i]);
	
	for(int i = 0; i <= MAX_T; i++){
		update = false;
		for(int j = 0; j < N; j++){
			if(A[j] <= i && B[j] >= i && (!update || C[j] > MAX[i])){
				update = true;
				MAX[i] = C[j];
			}
		}
	}
	for(int i = 0; i <= MAX_T; i++){
		update = false;
		for(int j = 0; j < N; j++){
			if(A[j] <= i && B[j] >= i && (!update || C[j] < MIN[i])){
				update = true;
				MIN[i] = C[j];
			}
		}
	}
	
	dp[0][0] = 0; dp[1][0] = 0;
	for(int i = 1; i < D; i++){
		num1 = abs(MAX[T[i]] - MIN[T[i - 1]]) + dp[1][i - 1];
		num2 = abs(MAX[T[i]] - MAX[T[i - 1]]) + dp[0][i - 1];
		dp[0][i] = max(num1, num2);
		
		num1 = abs(MIN[T[i]] - MIN[T[i - 1]]) + dp[1][i - 1];
		num2 = abs(MIN[T[i]] - MAX[T[i - 1]]) + dp[0][i - 1];
		dp[1][i] = max(num1, num2);
	}
	
	printf("%d\n", max(dp[0][D - 1], dp[1][D - 1]));
	
	/*for(int i = 0; i < D; i++){
		printf("%d %d\n", dp[0][i], dp[1][i]);
	}*/
	
	return 0;
}

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

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

int rec(int day, int prev)
{
	if(day == D) return 0;

	
}

int main()
{
	memset(dp, 0, sizeof dp);
	
	cin >> D >> N;

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

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

	for(int day = 0; day < D-1; day++) {
		for(int now = 0; now < N; now++) {
			for(int next = 0; next < N; next++) {
				if(A[now] <= T[day] && T[day] <= B[now] && A[next] <= T[day+1] && T[day+1] <= B[next]) {
					dp[day+1][next] = max(dp[day+1][next], dp[day][now] + abs(C[now] - C[next]));
				}
			}
		}
	}

	int ans = 0;
	for(int i = 0; i < N; i++) {
		ans = max(ans, dp[D-1][i]);
	}

	cout << ans << endl;
}

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

#define reep(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) reep((i),0,(n))
typedef pair<int,int> pii;
#define F first
#define S second
typedef vector<int> vint;
const int INF=INT_MAX/2;
int main(){
	int D,n;
	cin>>D>>n;
	vint temp(D);
	rep(i,D) cin>>temp[i];
	vector<pair<pii,int>> v(n);
	rep(i,n) cin>>v[i].F.F>>v[i].F.S>>v[i].S;
	vector<vector<int>> dp(D,vector<int>(n,0));
	rep(i,D){
		if(!i){
			rep(j,n){
				if(v[j].F.F<=temp[i]&&temp[i]<=v[j].F.S){
					dp[i][j]=0;
				}
				else{
					dp[i][j]=-INF;
				}
			}
			continue;
		}
		rep(j,n){
			if(v[j].F.F<=temp[i]&&temp[i]<=v[j].F.S){
				rep(k,n){
					dp[i][j]=max(dp[i][j],dp[i-1][k]+abs(v[k].S-v[j].S));
				}
			}
		}
	}
	int ans=0;
	rep(i,n){
		ans=max(ans,dp[D-1][i]);
	}
	cout<<ans<<endl;
}

C++: #include<iostream>

using namespace std;

struct clo{
	int l,h;
	int v;
};

int abs(int x){
	if(x<0)return x*-1;
	return x;
}

int tem[300];
clo data[300];
int dp[300][300]={0};

bool che(clo c,int t){
	if(c.l<=t && c.h>=t)return true;
	else return false;
}
int main(){
	int D,n;
	cin>>D>>n;
	for(int i=0;i<D;i++){
		cin>>tem[i];
	}
	for(int i=0;i<n;i++){
		cin>>data[i].l>>data[i].h>>data[i].v;
	}
	for(int i=0;i<n;i++){
		dp[0][i]=0;
	}
	for(int i=1;i<D;i++){
		for(int j=0;j<n;j++){
			for(int k=0;k<n;k++){
				if(che(data[k],tem[i]) && che(data[j],tem[i-1]))dp[i][k]=max(dp[i][k],dp[i-1][j]+abs(data[k].v-data[j].v));
			}
		}
	}
	int ans=0;
	for(int i=0;i<n;i++){
		ans=max(ans,dp[D-1][i]);
	}
	cout<<ans<<endl;
	
}

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

using namespace std;

int d,n;
int tenki[202];
int saiko[101],saitei[101];
int hade[101];
int dp[201][101];

int abs(int n){
	return n>0?n:-1;
}

int hot(int d,int f){
	if(dp[d][f]!=0)return dp[d][f];
	if(d==0){
		cout<<d<<"a "<<f<<" "<<endl;
		return dp[d][f]=0;
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(saitei[i]<=tenki[d]&&tenki[d]<=saiko[i]){
			ans=max(ans,hot(d-1,i)+abs(hade[i]-hade[f]));
		}
		cout<<d<<"asi "<<f<<" "<<ans<<endl;
	}
	cout<<d<<"as "<<f<<" "<<ans<<endl;
	return dp[d][f]=ans;
}

int main(){
	cin>>d>>n;
	for(int i=1;i<=d;i++){
		cin>>tenki[i];
	}
	for(int i=1;i<=n;i++){
		cin>>saitei[i]>>saiko[i]>>hade[i];
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(saitei[i]<=tenki[d-1]&&tenki[d-1]<=saiko[i]){
			ans=max(ans,hot(d-1,i));
		}
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF -9999999

using namespace std;

int main ()
{
	int d, n, a[205], b[205], c[205], t[205], dp[205][205], ans=0;
	memset(dp, 0, sizeof(dp));
	cin>>d>>n;
	for (int i=1; i<=d; i++) cin>>t[i];
	for (int i=1; i<=n; i++) cin>>a[i]>>b[i]>>c[i];
    for (int i=1; i<=n; i++) if (t[1]<a[i] || t[1]>b[i]) dp[1][i]=INF;
    for (int i=2; i<=d; i++)
    {
        for (int j=1; j<=n; j++)
        {
            if (t[i]<a[j] || t[i]>b[j]) dp[i][j]=INF;
            else
            {
                for (int k=1; k<=n; k++)
                {
                    dp[i][j]=max(dp[i][j], dp[i-1][k]+abs(c[j]-c[k]));
                }
            }
        }
    }
    for (int i=1; i<=n; i++) ans=max(ans, dp[d][i]);
    cout<<ans<<'\n';
}