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 <cstdio>
#include <cmath>
#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)

struct Clothes {
	int a, b, c;
};

int main() {
	int dp[201][200] = {};
	int d, n, mt[200];
	Clothes clothes[200];

	cin >> d >> n;
	REP(i, d) scanf("%d", &mt[i]);
	REP(i, n) scanf("%d %d %d", &clothes[i].a, &clothes[i].b, &clothes[i].c);
	
	REP(i, d) {
		REP(j, n) {
			if (clothes[j].a > mt[i] || clothes[j].b < mt[i]) continue;
			REP(k, n) {
				if (clothes[k].a > mt[i+1] || clothes[k].b < mt[i+1]) continue;
				dp[i+1][k] = max(dp[i+1][k], dp[i][j] + abs(clothes[k].c - clothes[j].c));
			}
		}
	}

	int ans = 0;
	REP(i, n) ans = max(ans, dp[d - 1][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
	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++){
		if(abs(MAX[T[i]] - MIN[T[i - 1]]) > abs(MAX[T[i]] - MAX[T[i - 1]])){
			dp[0][i] = abs(MAX[T[i]] - MIN[T[i - 1]]) + dp[1][i - 1];
		}
		else if(abs(MAX[T[i]] - MIN[T[i - 1]]) < abs(MAX[T[i]] - MAX[T[i - 1]])){
			dp[0][i] = abs(MAX[T[i]] - MAX[T[i - 1]]) + dp[0][i - 1];
		}
		else{
			dp[0][i] = abs(MAX[T[i]] - MAX[T[i - 1]]) + max(dp[0][i - 1], dp[1][i - 1]);
		}
		if(abs(MIN[T[i]] - MIN[T[i - 1]]) > abs(MIN[T[i]] - MAX[T[i - 1]])){
			dp[1][i] = abs(MIN[T[i]] - MIN[T[i - 1]]) + dp[1][i - 1];
		}
		else if(abs(MIN[T[i]] - MIN[T[i - 1]]) < abs(MIN[T[i]] - MAX[T[i - 1]])){
			dp[1][i] = abs(MIN[T[i]] - MAX[T[i - 1]]) + dp[0][i - 1];
		}
		else{
			dp[1][i] = abs(MIN[T[i]] - MAX[T[i - 1]]) + max(dp[0][i - 1], dp[1][i - 1]);
		}
	}
	
	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++: // 2013/11/09 Tazoe

#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;

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

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

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

	bool S[200][200];
	for(int i=0; i<D; i++){
		for(int j=0; j<N; j++){
			S[i][j] = (A[j]<=T[i] && T[i]<=B[j]);
		}
	}

	int DP[200][200];
	for(int i=0; i<D; i++){
		for(int j=0; j<N; j++){
			DP[i][j] = -1;
		}
	}

	for(int j=0; j<N; j++){
		if(S[0][j]){
			DP[0][j] = 0;
		}
	}

	for(int i=1; i<D; i++){
		for(int j=0; j<N; j++){
			if(!S[i][j])
				continue;

			for(int k=0; k<N; k++){
				if(!S[i-1][k])
					continue;

				DP[i][j] = max(DP[i][j], DP[i-1][k]+abs(C[k]-C[j]));
			}
		}
	}

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

	cout << ans << endl;

	return 0;
}

C++: #include<iostream>
#include<cmath>
using namespace std;
int D, N, T[200], A[200], B[200], C[200], dp[200][200];
int rec(int today,int atai){
  if(today == D) return 0;
  if(dp[today][atai]) return dp[today][atai];
  int ret = 0;
  for(int i = 0 ; i < N ; i++ ){
    if(T[today] >= A[i] && T[today] <= B[i]){
      ret = max( ret, rec( today + 1, C[i]) + (today ? abs(atai - C[i]) : 0));
    }
  }
  return dp[today][atai] = ret;
}
int main(){
  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];
  cout << rec(0,0) << endl;
}

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

const int A = 0;
const int B = 1;
const int C = 2;

int D, N;
int T[200];
int F[200][3];  //Fuku

int DP[200][200];

int main(void) {
  cin >> D >> N;
  for (int i=0; i<D; i++) {
    cin >> T[i];
  }
  for (int j=0; j<N; j++) {
    cin >> F[j][A] >> F[j][B] >> F[j][C];
  }
  for (int i=0; i<D; i++) {
    for (int j=0; j<N; j++) {
      DP[i][j] = -1;
    }
  }
  for (int j=0; j<N; j++) {
    if (F[j][A]<=T[0] && F[j][B]>=T[0]) {
      DP[0][j] = 0;
    }
  }
  for (int i=0; i<D-1; i++) {
    for (int j=0; j<N; j++) {
      if (DP[i][j] != -1) {
	for (int k=0; k<N; k++) {
	  if (F[k][A]<=T[i+1] && F[k][B]>=T[i+1]) {
	    DP[i+1][k] = max(DP[i+1][k], abs(F[j][C]-F[k][C])+DP[i][j]);
	  }
	}
      }
    }
  }
  int ans = 0;
  for (int j=0; j<N; j++) {
    ans = max(ans, DP[D-1][j]);
  }
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

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

int dfs(int d, int prev){
  //  printf("%d %d\n", d, prev);
  if(d == D) return 0;
  if(dp[d][prev] >= 0) return dp[d][prev];

  int res = 0;
  rep(i, N){
    if(A[i] > T[d] || B[i] < T[d]) continue;
    res = max(res, dfs(d+1, i) + abs(C[i] - C[prev]));
  }
  return dp[d][prev] = res;
}

int main(){
  scanf("%d%d", &D, &N);
  rep(i, D) scanf("%d", T+i);
  rep(i, N) scanf("%d%d%d", A + i, B + i, C + i);
  //  memset(dp, -1, sizeof(dp));

  //  int res = 0;
  //  rep(i, N) if(A[i] <= T[0] && T[0] <= B[i]) res = max(res, dfs(1, i));
  //  printf("%d\n", res);

  memset(dp, 0, sizeof(dp));
  rep(i, D){
    rep(j, N){
      rep(k, N){
	if(A[k] <= T[i+1] && B[k] >= T[i+1] && A[j] <= T[i] && B[j] >= T[i])
	  dp[i+1][k] = max(dp[i+1][k], dp[i][j] + abs(C[j] - C[k]));
	else
	  dp[i+1][k] = dp[i+1][k];
      }
    }
  }

  /*  rep(i, D+1){
    rep(j, N) printf("%d ", dp[i][j]); puts("");
    }*/
  int res = 0;
  rep(i, N) res = max(res, dp[D-1][i]);
  printf("%d\n", res);
  return 0;
}