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 <iostream>
#include <algorithm>
using namespace std;
int d, n;
int t[205] = { 0 }, a[205] = { 0 }, b[205] = { 0 }, c[205] = { 0 };
int dp[205][205] = { 0 };
int main() {
	cin >> d >> n;
	for (int i = 1; i <= d; i++) {
		cin >> t[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i] >> c[i];
	}
	for (int i = 2; i <= d; i++) {
		for (int j = 0; j < n; j++) {
			if (a[j] <= t[i] && t[i] <= b[j]) {
				for (int k = 0; k < n; k++) {
					if(a[k]<=t[i-1]&&t[i-1]<=b[k]) dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(c[k] - c[j]));
				}
			}
			else continue;
		}
	}
	int res = 0;
	for (int j = 0; j < n; j++) {
		res = max(res, dp[d][j]);
	}
	cout << res << endl;
	return 0;
}

C++: #include "bits/stdc++.h"
#define rep(i,a,n) for(int i = a;i < n;i++)
typedef unsigned long long ull;
typedef long long ll;
using namespace std;

int main(){
    int n,m,memo = 0;
    cin >> n >> m;
    int t[n],a[m],b[m],c[m],dp[n][m] = {0};
    
    
    rep(i,0,n){
        cin >> t[i];
    }
    rep(i,0,m){
        cin >> a[i] >> b[i] >> c[i];
    }
    rep(i,0,n){
        rep(j,0,m){
            dp[i][j] = 0;
        }
    }
    
    rep(i,1,n){ 
        rep(j,0,m){ //前日
            rep(k,0,m){//当日
                if(a[j] <= t[i-1] && t[i-1] <= b[j] && a[k] <= t[i] && t[i] <= b[k]){
                    dp[i][k] = max(dp[i][k],abs(c[j]-c[k])+dp[i-1][j]);
                }
            }
        }
    }
    /*
    rep(i,0,n){
        rep(j,0,m){
            cout << dp[i][j] << " ";  
        }
        cout << endl;
    }
    */
    
    rep(i,0,m){
        memo = max(memo,dp[n-1][i]);
    }
    
    
    
    cout << memo << endl;
    
    return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

int D, N, T[212], A[212], B[212], C[212];
int memo[212][112];

int dp(int d, int b)
{
	if (memo[d][b] != -1) { return memo[d][b]; }
	if (d < 0) { return memo[d][b] = 0; }
	int res = 0;
	rep(i, N)
	{
		if (A[i] <= T[d] && T[d] <= B[i])
		{
			memo[d - 1][C[i]] = dp(d - 1, C[i]) + abs(b - C[i]);
			if (d == D - 1) { memo[d - 1][C[i]] -= abs(b - C[i]); }
			res = std::max(res, memo[d - 1][C[i]]);
		}
	}
	return memo[d][b] = res;
}

int main()
{
	memset(memo, -1, sizeof(memo));
	in >> D >> N;
	rep(i, D) { in >> T[i]; }
	rep(i, N) { in >> A[i] >> B[i] >> C[i]; }
	out << dp(D - 1, 0) << std::endl;
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
typedef pair<int,int> P;
typedef pair <int, P> P2;
#define mp make_pair
#define ft first
#define sd second

int d, n;
int t[200];
P2 p[200];
P c[200];
int s[200][2];

int main() {
	
	cin >> d >> n;
	
	REP(i, d) cin >> t[i];
	REP(i, n) cin >> p[i].sd.ft >> p[i].sd.sd >> p[i].ft;
	
	sort(p, p+n);
	
	int k;
	REP(i, d) {
		k = 0;
		while(t[i] < p[k].sd.ft || t[i] > p[k].sd.sd) {
			k++;
		}
		c[i].ft =  p[k].ft;
	}
	
	k = n-1;
	REP(i, d) {
		k = n-1;
		while(t[i] < p[k].sd.ft || t[i] > p[k].sd.sd) {
			k--;
		}
		c[i].sd =  p[k].ft;
	}
	
	s[0][0] = 0;
	s[0][1] = 0;
	
	FOR(i, 1, d) {
		s[i][0] = max(s[i-1][0]+abs(c[i-1].ft-c[i].ft), s[i-1][1]+abs(c[i-1].sd-c[i].ft));
		s[i][1] = max(s[i-1][0]+abs(c[i-1].ft-c[i].sd), s[i-1][1]+abs(c[i-1].sd-c[i].sd));
	}
	
	int res = max(s[d-1][0], s[d-1][1]);
	
	cout << res << endl;
	
	return 0;
}

C++: 
#include<algorithm>
#include<iostream>
#include<cstring>

using namespace std;

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

int f(int d, int c) {
  int i;

  if(d == D)
    return 0;
  if(M[d][c] >= 0)
    return M[d][c];

  int mx = 0;
  for(i = 0; i < N; ++i) {
    if(A[i] <= T[d] && T[d] <= B[i])
      mx = max(mx, abs(C[i] - c) + f(d + 1, C[i]));
  }

  return M[d][c] = mx;
}

int main() {
  int i;

  cin >> D >> N;
  for(i = 0; i < D; ++i)
    cin >> T[i];
  for(i = 0; i < N; ++i)
    cin >> A[i] >> B[i] >> C[i];

  memset(M, -1, sizeof(M));

  int mx = 0;
  for(i = 0; i < N; ++i) {
    if(A[i] <= T[0] && T[0] <= B[i])
      mx = max(mx, f(1, C[i]));
  }

  cout << mx << endl;
  return 0;
}

C++: /*
D??\??????????°??????±
???????°???????T???
N?¨??????????
???j??????????°???? A??\??? B??\???????´??????? C
??£?¶?????????\????????????????´????????????¶?????????????¨?
*/
/*
1.????????????????±???????
2.????????\??¨?¬??????\??????????????§?????§??????????±??????????
3.???????????°????????????
4.?????????????????????????????????????????????
*/
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int main(){
	int D, N, T[201], A[201], B[201], C[201], max_day = 0;
	int dp[201][201];  //[????????\][?????????]
	bool wear[200][200];//[????????\][?????????] ????????\???????????¨????????\?????????true
	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 i = 0; i < D; i++){
		for (int j = 0; j < N; j++){
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < D; i++){
		for (int j = 0; j < N; j++){
			if (A[j] <= T[i] && B[j] >= T[i]){//??????????????????????????????
				dp[i][j] = 0;
			}
		}
	}
	for (int i = 0; i < D; i++){
		for (int j = 0; j < N; j++){
			if (dp[i][j] >= 0){
				for (int k = 0; k < N; k++){
					if (dp[i + 1][k] >= 0){//k????????????????????????
						dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(C[j] - C[k]));//?´????????????¶????????´??°
					}
				}
			}
		}
	}
	for (int i = 0; i < D; i++){
		if (dp[D - 1][i] > max_day){
			max_day = dp[D - 1][i];//?????§??¶????????´??°
		}
	}
	cout << max_day << endl;
	return 0;
}