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 <cstdio>
#include <utility>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
#define _OP(t) inline operator t()
struct _in {
#ifdef _GLIBCXX_STRING
	_OP(string){string s;for(char c;c=gcu(),c!=' '&&c!='\n';)s+=c;return s;}
#endif
	_OP(char){char c=gcu();gcu();return c;}
	_OP(double){double d; scanf("%lf",&d); gcu();return d;}
	_T _OP(T){T n{},m{1},c;if((c=gcu())=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
} in;
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_SCAN(string &o) {int c{gcu()};if(c==EOF)return false;else{ungetc(c,stdin);string t=move(in);o=t;return true;}}
_T _SCAN(T &o) {int c{gcu()};return c==EOF?false:(ungetc(c,stdin),o=in,true);}
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);}
_OUT(const char *s){while(*s)pcu(*s++);}
_OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_OUT(string &s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _OUT(H &&h, T... t){out(h);out(t...);}
template <typename... T> _OUTL(T... t){out(t...);outl();}
struct range{
	int e,b=0,s=1;range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
	struct it{int v,s; it(int _v,int _s):v(_v),s(_s){} operator int()const{return v;} operator int&(){return v;}int operator*()const{return v;}
		it& operator++(){v+=s;return *this;} }; it begin(){return {b,s};} it end(){return {e,s};}};
#define times(i, n) for(int i{},i##_e{n};i<i##_e;i++)

struct d {int c, a;};

int main() {
	int D {in}, N {in};
	vector<int> T(D);
	array<vector<int>, 61> W {};
	vector<vector<d>> dp(D);
	for (int &i: T)
		i = in;
	times (i, N)
		for (int j {in}, e {in}, c {in}; j <= e; j++)
			W[j].push_back(c);
	for (int i: W[T[0]])
		dp[0].push_back({i, 0});
	for (int i: range(1, D))
		for (int j: W[T[i]]) {
			int a {};
			for (d k: dp[i - 1])
				a = max(a, abs(j - k.c) + k.a);
			dp[i].push_back({j, a});
		}
	int r {};
	for (d i: dp.back())
		r = max(r, i.a);
	outl(r);
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;

int D,N;
int T[211];
vector<int> Ds[211];

inline int ab(int a){ return a>0?a:-a; }
int dp[211][101];
int solve(int day,int c){
  if(dp[day][c]!=-1) return dp[day][c];
  if(day==D) return 0;
  int ret=0;
  for(int i=0;i<(int)Ds[day].size();i++){
    ret = max( ret, solve(day+1,Ds[day][i]) + ab(c-Ds[day][i]) );
  }
  //  printf("%d\n",ret);
  return dp[day][c]=ret;
}
int solve(){
  int ret = 0;
  memset(dp,-1,sizeof(dp));
  for(int i=0;i<(int)Ds[0].size();i++){
    ret = max( ret, solve(1,Ds[0][i]) );
  }
  return ret;
}
int main(){
  scanf("%d %d",&D,&N);
  for(int i=0;i<D;i++){
    scanf("%d",&T[i]);
  }
  for(int i=0;i<N;i++){
    int A,B,C;
    scanf("%d %d %d",&A,&B,&C);
    for(int j=0;j<D;j++){
      if(A<=T[j] && T[j]<=B) Ds[j].push_back(C);
    }
  }
  printf("%d\n",solve());
}

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

using namespace std;

#define MAX 100
#define INF 1e9

int main() {
	int d, n; cin >> d >> n;
	vector<int> t(d), a(n), b(n), c(n);
	vector < vector<int> > dp(d, vector<int>(MAX + 1, -INF));
	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) {
			if (a[j] <= t[i] && t[i] <= b[j]) {
				if (i == 0) {
					dp[i][c[j]] = 0;
					continue;
				}
				for (int k = 0; k <= MAX; ++k) {
					dp[i][c[j]] = max(dp[i][c[j]], dp[i - 1][k] + abs(k - c[j]));
				}
			}
		}
	}
	cout << *max_element(dp[d - 1].begin(), dp[d - 1].end()) << endl;

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int D, N, T[200], A[200], B[200], C[200];
int dp[300][300];

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];

  fill_n(*dp, 300 * 300, -1);
  for(int i = 0; i < N; i++) {
    if(B[i] >= T[0] && T[0] >= A[i]) {
      for(int j = 0; j < N; j++) {
        if(B[j] >= T[1] && T[1] >= A[j]) {
          dp[1][j] = max(C[j] - C[i], C[i] - C[j]);
        }
      }
    }
  }

  for(int i = 2; i < D; i++) {
    for(int j = 0; j < N; j++) {
      if(~dp[i - 1][j]) {
        for(int k = 0; k < N; k++) {
          if(B[k] >= T[i] && T[i] >= A[k]) {
            dp[i][k] = max(dp[i][k], dp[i - 1][j] + max(C[j] - C[k], C[k] - C[j])); 
          }
        }
      }
    }
  }
  cout << *max_element(dp[0], dp[300]) << endl;
}

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

using namespace std;

#define INF (1<<28)
#define MD 200
#define MN 100
int D, N;
int T[MD+1];
int CA[MN+1], CB[MN+1], CC[MN+1];

int solve() {
    int dp[MD+1][MN+1];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < N; j++) {
            if (not (CA[j] <= T[i] && T[i] <= CB[j])) dp[i][j] = -1;
        }
    }
    for (int i = 1; i < D; i++) {
        for (int j = 0; j < N; j++) {
            if (dp[i][j] < 0) continue;
            for (int k = 0; k < N; k++) {
                if (dp[i-1][k] < 0) continue;
                dp[i][j] = max(dp[i][j], dp[i-1][k] + abs(CC[j] - CC[k]));
            }
        }
    }
    int ret = 0;
    for (int i = 0; i < N; i++) {
        ret = max(ret, dp[D-1][i]);
    }
    //for (int i = 0; i < D; i++) {
    //    for (int j = 0; j < N; j++) {
    //        printf("%4d", dp[i][j]);
    //    }
    //    printf("\n");
    //}
    return ret;
}

int main() {
    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", CA+i, CB+i, CC+i);
    printf("%d\n", solve());
    return 0;
}