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 <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
	int d, n;
	cin >> d >> n;

	vector<int> t(d);
	rep(i, d) cin >> t[i];

	vector<int> a(n), b(n), c(n);
	rep(i, n) cin >> a[i] >> b[i] >> c[i];

	int dp[205][205];
	rep(i, 205) rep(j, 205) dp[i][j] = -1;

	rep(i, n) {
		if(a[i] <= t[0] && t[0] <= b[i]) dp[0][i] = 0;
	}

	REP(i, 1, d) {
		rep(j, n) {
			if(a[j] <= t[i] && t[i] <= b[j]) {
				int res = -1, id = -1;
				rep(k, n) {
					if(dp[i-1][k] != -1 && res < abs(c[k] - c[j])) {
						res = abs(c[k] - c[j]);
						id = k;
					}
				}

				dp[i][j] = dp[i-1][id] + res;
			}
		}
	}

	// rep(i, d + 1) {
	// 	rep(j, n) cout << dp[i][j] << " ";
	// 	cout << endl;
	// }

	int ans = 0;
	rep(i, n) {
		ans = max(ans, dp[d-1][i]);
	}

	cout << ans << endl;


	return 0;
}

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

int dp[201][201], d, n, t[201], a[201], b[201], c[201], mx = 0;

int abs(int x, int y)
{
	if (x >= y) return x - y;
	else return y - x;
}

int main()
{
	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];
	}
	memset(dp, -1, sizeof dp);

	for (int i = 1; i <= n; i++){
		if (a[i] <= t[1] && t[1] <= b[i]){
			dp[1][i] = 0;
		}
	}

	//dp
	for (int today = 2; today <= d; today++){
		for (int n1 = 1; n1 <= n; n1++){
			if (dp[today - 1][n1] == -1) continue;
			for (int n2 = 1; n2 <= n; n2++){
				if (a[n2] > t[today] || t[today] > b[n2]) continue;
				dp[today][n2] = max(dp[today][n2], dp[today - 1][n1] + abs(c[n1], c[n2]));
			}
		}
	}

	for (int i = 1; i < n; i++){
		mx = max(dp[d][i], mx);
	}
	cout << mx << endl;
	return 0;
}

C++: #include<stdio.h>
int dp[201][201];
int main(void)
{
	int D,N,T[201],A[201],B[201],C[201],i,j,k,max=0,x=0;
	scanf("%d%d",&D,&N);
	for(i=1;i<=D;i++){
		scanf("%d",&T[i]);
	}
	for(i=1;i<=N;i++){
		scanf("%d%d%d",&A[i],&B[i],&C[i]);
	}
	for(i=1;i<=D;i++){
		for(j=1;j<=N;j++){
			dp[i][j]=-1;
		}
	}
	for(i=1;i<=N;i++){
		dp[1][i]=0;
	}
	for(i=1;i<D;i++){
		for(j=1;j<=N;j++){
			if(dp[i][j]!=-1){
				if(A[j]<=T[i]&&T[i]<=B[j]){
					for(k=1;k<=N;k++){
						if(A[k]<=T[i+1]&&T[i+1]<=B[k]){
							if(C[j]-C[k]<0){
								x=-1*(C[j]-C[k]);
							}
							else{
								x=C[j]-C[k];
							}
						}
						if(dp[i+1][k]<dp[i][j]+x){
							dp[i+1][k]=dp[i][j]+x;
						}
					}
				}
			}
		}
	}
	/*for(i=1;i<=D;i++){
		for(j=1;j<=N;j++){
			printf("%d ",dp[i][j]);
		}
		printf("\n");
	}*/
	for(i=1;i<=N;i++){
		if(max<dp[D][i]){
			max=dp[D][i];
		}
	}
	printf("%d\n",max);
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

int main()
{
	for(int d,n;cin>>d>>n;){
		vi ts(d),as(n),bs(n),cs(n);
		rep(i,d) cin>>ts[i];
		rep(i,n) cin>>as[i]>>bs[i]>>cs[i];
		
		vvi dp(d,vi(n,-1));
		rep(i,n) if(as[i]<=ts[0] && ts[0]<=bs[i])
			dp[0][i]=0;
		repi(i,1,d) rep(j,n) if(as[j]<=ts[i] && ts[i]<=bs[j])
			rep(k,n) if(dp[i-1][k]!=-1)
				dp[i][j]=max(dp[i][j],dp[i-1][k]+abs(cs[j]-cs[k]));
		cout<<*max_element(all(dp[d-1]))<<endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void)
{
	int D,N;
	int i,j,k;
	int dp[201][201];
	int T[201],A[201],B[201],C[201];
	
	cin>>D>>N;
	
	for(i=1;i<=D;i++){
		cin>>T[i];
	}
	
	for(i=1;i<=N;i++){
		cin>>A[i]>>B[i]>>C[i];
	}
	
	for(i=1;i<=D;i++){
		for(j=1;j<=N;j++){
			dp[i][j]=-1;
		}
	}
	
	
	for(i=1;i<=N;i++){
		if(A[i]<=T[1]&&T[1]<=B[i]){
			dp[1][i]=0;
		}
	}
	
	for(i=1;i<=D;i++){
		for(j=1;j<=N;j++){
			cout<<dp[i][j];
		}
		cout<<endl;
	}
	
	int sum=0;
	for(i=1;i<D;i++){
		for(j=1;j<=N;j++){
			if(dp[i][j]!=-1){
				for(k=1;k<=N;k++){
					if(A[k]<=T[i+1]&&T[i+1]<=B[k]){
						sum=abs(C[j]-C[k]);
						dp[i+1][k]=max(dp[i+1][k],dp[i][j]+sum);
					}					
				}
			}
		}
	}
	
	for(i=1;i<=D;i++){
		for(j=1;j<=N;j++){
			cout<<dp[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}