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 <map>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define fr first
#define sc second
using namespace std;

int d,n;
int tem[201],dp[201][101];
typedef pair<int,int> P;
typedef pair<P,int> iP;
iP clt[201];

int rec(int idx,int prev){
  if(idx == d) return 0;
  if(dp[idx][prev] != -1) return dp[idx][prev];
  int res = 0;
  for(int i=0;i<n;i++){
    if(clt[i].fr.fr <= tem[idx] && tem[idx] <= clt[i].fr.sc){
      res = max(res,rec(idx+1,clt[i].sc) + abs(clt[i].sc - prev));
    }
  }
  return dp[idx][prev] = res;
}
  

int main(){
  memset(dp,-1,sizeof(dp));
  cin >> d >> n;
  for(int i=0;i<d;i++) cin >> tem[i];
  for(int i=0;i<n;i++) cin >> clt[i].fr.fr >> clt[i].fr.sc >> clt[i].sc;
  int ans = 0;  
  for(int i=0;i<n;i++){
    if(clt[i].fr.fr <= tem[0] && tem[0] <= clt[i].fr.sc){ 
      ans = max(ans,rec(1,clt[i].sc));
    }
  }
  cout << ans << endl;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define int long long

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int T[200], A[200], B[200], C[200];

int dp[201][200];

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

    rep(i, D) {
        cin >> T[i];
    }

    rep(i, N) {
        cin >> A[i] >> B[i] >> C[i];
    }

    for (int i = 1; i <= D; ++i) {
        rep(j, N) {
            if (T[i - 1] < A[j] || T[i - 1] > B[j]) continue;

            rep(k, N) {
                if (T[i] < A[k] || T[i] > B[k]) continue;

                chmax(dp[i][k], dp[i - 1][j] + abs(C[j] - C[k]));
            }
        }
    }

    int ans = 0;

    rep(i, D + 1) rep(j, N) chmax(ans, dp[i][j]);

    cout << ans << endl;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#define INF (1145141919)
using namespace std;

typedef struct
{
	int mi;
	int ma;
	int f;
} CLOTHES;

CLOTHES clothes[1000];
int T[1000];
int dp[1000][1000];

int main()
{
	memset(dp, -1, sizeof(dp));
	for (int i = 0; i < 1000; i++){
		dp[0][i] = 0;
	}
	
	int D, N;
	
	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", &clothes[i].mi, &clothes[i].ma, &clothes[i].f);
	}
	
	for (int i = 0; i < D; i++){
		for (int j = 0; j < N; j++){
			for (int k = 0; k < N; k++){
				if (T[i] >= clothes[k].mi && T[i] <= clothes[k].ma && dp[i][j] != -1){
					if (i > 0){
						dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(clothes[j].f - clothes[k].f));
					}
					else {
						dp[i + 1][k] = 0;
					}
				}
			}
		}
	}
	
	int maxi = -INF;
	
	for (int i = 0; i < N; i++){
		maxi = max(maxi, dp[D][i]);
	}
	
	printf("%d\n", maxi);
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define times(n, i)      uptil(0, n, i)
#define upto(f, t, i)    for(decltype(t) _##i = (t), i = (f); i <= _##i; i++)
#define uptil(f, t, i)   for(decltype(t) _##i = (t), i = (f); i < _##i; i++)
#define downto(f, t, i)  for(decltype(t) _##i = (t), i = (f); i >= _##i; i--)
#define downtil(f, t, i) for(decltype(t) _##i = (t), i = (f); i > _##i; i--)
#define unless(c) if(!(c))
#define until(c) while(!(c))
#define loop while(true)

#define all(v) v.begin(), v.end()
#define ln << "\n"

struct line{};

template<class T> istream& operator>>(istream& in, vector<T>& v) {
    times(v.capacity(), i) in >> v[i];
    return in;
}

template<class T> const ostream& operator<<(ostream& out, const vector<T>& v) {
    times(v.size(), i) out << (i ? " " : "") << v[i];
    return out;
}

template<class T, class...U> typename enable_if<is_same<T, line>::value, string>::type next(istream& in, const U&... args) {
    string s;
    getline(in, s, args...);
    return move(s);
}

template<class T, class...U> typename enable_if<!is_same<T, line>::value, T>::type next(istream& in, const U&... args) {
    T t(args...);
    in >> t;
    return move(t);
}
template<class T, class...U> auto next(const U&... args) -> decltype(next<T>(cin)) {
    return next<T>(cin, args...);
}

template<class T> T sum(const vector<T>& v) {
    T ans = T();
    for(const T& t : v) ans += t;
    return ans;
}

template<class T> T sum(const typename vector<T>::iterator& begin, const typename vector<T>::iterator& end) {
    T ans = T();
    for(typename vector<T>::iterator t = begin; t != end; t++) ans += *t;
    return ans;
}

template<class T> T min(const vector<T>& v) {
    return *min_element(v.begin(), v.end());
}

template<class T> T min(const typename vector<T>::iterator& begin, const typename vector<T>::iterator& end) {
    return *min_element(begin, end);
}

template<class T> T max(const vector<T>& v) {
    return *max_element(v.begin(), v.end());
}

template<class T> T max(const typename vector<T>::iterator& begin, const typename vector<T>::iterator& end) {
    return *max_element(begin, end);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int D = next<int>(), N = next<int>();
    auto T = next<vector<int>>(D);
    vector<vector<int>> c(D);
    times(N, j) {
        int A = next<int>(), B = next<int>(), C = next<int>();
        times(D, i) {
            if(A <= T[i] && T[i] <= B) c[i].push_back(C);
        }
    }
    int ans_max, before_max, ans_min, before_min;
    times(D, i) {
        int a = min(c[i]), b = max(c[i]);
        if(i) {
            ans_min = max(ans_min + abs(a - before_min), ans_max + abs(a - before_max));
            before_min = a;
            ans_max = max(ans_min + abs(b - before_min), ans_max + abs(b - before_max));
            before_max = b;
        } else {
            ans_max = ans_min = 0;
            before_min = a;
            before_max = b;
        }
    }
    cout << max(ans_min, ans_max) ln;

    return 0;
}