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

constexpr int MAX=200;

int d,n;

int t[MAX];
int a[MAX],b[MAX],c[MAX];

int dp[MAX/*テヲツ猟・テ」ツ?ォテ」ツ?。*/][MAX/*テ、ツサツ甘ヲツ猟・*/];

int main() {
	std::cin>>d>>n;
	for(int i=0;i<d;++i)
		std::cin>>t[i];
	for(int i=0;i<n;++i)
		std::cin>>a[i]>>b[i]>>c[i];

	for(int i=1;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]=std::max(dp[i][j],dp[i-1][k]+std::abs(c[j]-c[k]));
				}
			}
		}
	}
	int ans=0;
	for(int i=0;i<n;++i)
		ans=std::max(ans,dp[d-1][i]);
	std::cout<<ans<<std::endl;
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <complex>
#include <string>
#include <sstream>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

#define all(c) c.begin(),c.end()
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

typedef long long ll;
typedef vector<int> vi;


template<typename T>
ostream& operator<<(ostream& os,const vector<T>& val){
    os << "[ ";
    for(typename vector<T>::const_iterator it=val.begin();
        it != val.end();++it){
        os << *it << " ";
    }
    os << "]";
    return os;
}

int main(){
    int D,N;
    cin >> D >> N;
    vector<int> T(D);
    vector<int> A(N),B(N),C(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];

    const int dame = -1;
    vector<int> maxi(N,dame);
    // first day.
    for(int i=0;i<N;i++){
        if(A[i] <= T[0] and T[0] <= B[i]){
            maxi[i] = 0;
        }
    }

    for(int i=1;i<D;i++){
        vector<int> next(N,dame);
        for(int mae=0;mae<N;mae++){
            if(maxi[mae] == dame) continue;
            for(int now=0;now<N;now++){
                if(A[now] <= T[i] and T[i] <= B[now]){
                    next[now] = max(next[now],maxi[mae]+abs(C[mae]-C[now]));
                }
            }
        }
        maxi = next;
    }
    cout << *max_element(all(maxi)) << endl;
    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, -1, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int D,N;cin>>D>>N;
  int T[D];FOR(i,0,D)cin>>T[i];
  int A[N],B[N],C[N];
  FOR(i,0,N)cin>>A[i]>>B[i]>>C[i];
  ll dp[D][N];
  CLR(dp);
  FOR(i,0,N){
    if(T[0]>=A[i]&&T[0]<=B[i]){
      dp[0][i]=0;
    }
  }
  ll ans=0;
  FOR(i,0,D){
    FOR(j,0,N){
      if(dp[i][j]==-1)continue;
      // i+1日目にkをきる
      FOR(k,0,N){
        if(T[i+1]>=A[k]&&T[i+1]<=B[k]){
          dp[i+1][k]=max(dp[i+1][k],dp[i][j]+abs(C[j]-C[k]));
          ans=max(ans,dp[i+1][k]);
        }
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>
#include <fstream>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)

#define delete(a,n) a.erase(remove(all(a),n),a.end())
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;} 
ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;}
string itos(int n){stringstream ss;ss << n;return ss.str();}

long long in(){
	long long n;
	scanf("%lld",&n);
	return n;
}

int A[201] , B[201] , C[201];

int T[201];
int dp[201][201] = {};
int N,M;

int dfs(int pos,int prev){
	if( pos == N ) return 0;
	if( dp[pos][prev] != -1 ) return dp[pos][prev];
	int ans = 0;
	for(int i = 0 ; i < M ; i++){
		if( A[i] <= T[pos] && T[pos] <= B[i] ){
			ans = max( ans , dfs(pos+1,C[i]) + abs(C[i]-prev)  );
		}
	}
	return dp[pos][prev] = ans;
}
//ok
int main(){
	memset(dp,-1,sizeof(dp));
	//ios_base::sync_with_stdio(false);
	cin >> N >> M;
	for(int i = 0 ; i < N ; i++){
		cin >> T[i];
	}
	for(int i = 0 ; i < M ; i++){
		cin >> A[i] >> B[i] >> C[i];
	}
	int ans = 0;
	for(int i = 0 ; i < M ; i++){
		if( A[i] <= T[0] && T[0] <= B[i] ){
			ans = max( ans , dfs(1,C[i]) );
		}
	}
	cout << ans << endl;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <queue>
using namespace std;
int hot[201] = { 0 };
int cloth_low[201] = { 0 };
int cloth_high[201] = { 0 };
int cloth_hade[201] = { 0 };
int dp[201][201] = { 0 };
int main() {
	int d, n;
	scanf("%d%d", &d, &n);
	for (int i = 1;i <= d;++i) scanf("%d", &hot[i]);
	for (int i = 1;i <= n;++i) scanf("%d%d%d", &cloth_low[i], &cloth_high[i], &cloth_hade[i]);
	for (int i = 2;i <= d;++i) {
		for (int backcloth = 1;backcloth <= n;++backcloth) {
			if (cloth_low[backcloth] <= hot[i-1] && hot[i-1] <= cloth_high[backcloth]) {
				for (int nowcloth = 1;nowcloth <= n;++nowcloth) {
					if (cloth_low[nowcloth] <= hot[i] && hot[i] <= cloth_high[nowcloth]) {
						int hoge = 0;
						if (cloth_hade[backcloth] < cloth_hade[nowcloth]) {
							 hoge = cloth_hade[nowcloth] - cloth_hade[backcloth];
						}
						else {
							hoge = cloth_hade[backcloth] - cloth_hade[nowcloth];
						}
						dp[i][nowcloth] = max(dp[i][nowcloth], dp[i - 1][backcloth] + hoge);
					}
				}
			}
		}
	}
	int maxes = 0;
	for (int i = 1;i <= n;++i) {
		maxes = max(maxes, dp[d][i]);
	}
	printf("%d\n", maxes);
}