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 <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#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 <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>
   
using namespace std;
   
 
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define rforeach(it, c) for (__typeof__((c).rbegin()) it=(c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
#define ten(n) ((long long)(1e##n))
#define bin(n) (1LL << (n))
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define revrep(i, n) for (int i = (n); i >= 0; --i)
#define pb push_back

template <class T> void chmax(T& a, const T& b) { a = max(a, b); }
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
 
template <class T> void uniq(vector<T>& c) { sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); }
 
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
  
template <class T, class U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os; }
  
template <class T> void print(T a, int n, const string& deli = " ", int br = 1) { for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } while (br--) cout << endl; }
template <class T> void print(const T& c, const string& deli = " ", int br = 1) { foreach (it, c) { cout << *it; if (++it != c.end()) cout << deli;--it; } while (br--) cout << endl; }
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) {    if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
   
template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }

void fix_pre(int n) { cout.setf(ios::fixed, ios::floatfield); cout.precision(10); }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
#define trace(x) (cout << #x << ": " << (x) << endl)
 
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }
 

typedef long long ll;
typedef pair<int, int> pint;
 
// y(v): v>^<  y(^): ^>v<
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };

const double PI = acos(-1.0);

#define F first
#define S second



int main()
{
    int d, n, t[222];
    cin >> d >> n;
    input(t, d);
    vector<int> cloth[222];
    while (n--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        rep(i, d)
            if (a <= t[i] && t[i] <= b)
                cloth[i].pb(c);
    }

    const int inf = ten(7);
    int dp[222][111];
    rep(i, d) erep(j, 100)
        dp[i][j] = -inf;
    rep(k, cloth[0].size())
        dp[0][cloth[0][k]] = 0;

    for (int i = 1; i < d; ++i)
        erep(j, 100) rep(k, cloth[i].size())
            chmax(dp[i][cloth[i][k]], dp[i - 1][j] + abs(cloth[i][k] - j));

    cout << *max_element(dp[d - 1], dp[d - 1] + 105) << endl;
}

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

const int INF = 1e9;
const int MAX_C = 100;

int main()
{
	int D, N;
	cin >> D >> N;
	vector<int> T(D), 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];
	}
	vector<vector<int>> dp(D, vector<int>(MAX_C + 1, -INF));
	for (int i = 0; i < N; i++) {
		if (A[i] <= T[0] && T[0] <= B[i]) {
			dp[0][C[i]] = 0;
		}
	}
	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 <= MAX_C; k++) {
					dp[i][C[j]] = max(dp[i][C[j]], dp[i - 1][k] + abs(k - C[j]));
				}
			}
		}
	}
	int res = 0;
	for (int i = 0; i <= MAX_C; i++) {
		res = max(res, dp[D - 1][i]);
	}
	cout << res << endl;
	return 0;
}

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

using namespace std;

int D,N,t,a,b,c;
vector<int>T,A,B,C;


int func(int i,int p,int res){
  
  if(i==D)return res;

  int mx=0;
  for(int k=0;k<N;k++){
    if(A[k]<=T[i] && T[i]<=B[k] && mx<abs(p-C[k])){
      mx=abs(p-C[k]);
    }
  }

  int ans=0;
  for(int j=0;j<N;j++){
    if(mx==abs(p-C[j])&& A[j]<=T[i] && T[i]<=B[j])ans=max(ans,func(i+1,C[j],res+mx));
  }
  return ans;  
}

int main(void){


  while(cin >> D >> N){
    T.clear();
    A.clear();
    B.clear();
    C.clear();
      
    for(int i=0;i<D;i++){
      cin >> t;
      T.push_back(t);
    }

    for(int i=0;i<N;i++){
      cin >> a >> b >> c;
      A.push_back(a);
      B.push_back(b);
      C.push_back(c);
    }

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

  return 0;
}

C++: #include<bits/stdc++.h>
#define LL long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)

using namespace std;

static const LL MAX_N = 1e+5;
static const LL INF = 1 << 25;

struct clothes{
    LL A[100],B[100],C[150];
};

int N,D;
LL res=-INF;
LL Day[300];
LL mem[300][100][100];
clothes T;

LL cal(LL day,LL p,LL pp,LL sum){
    //cout<<sum+abs(pp-p)<<" "<<day<<" "<<p<<" "<<pp<<endl;
    if(mem[day][p][pp]!=0){
        res=mem[day][p][pp];
        mem[day][p][pp]=0;
    }
    else{
        if(res<sum+abs(pp-p)&&day==N+1)mem[day][p][pp]=res=sum+abs(pp-p);
        if(day==N+1)return 0;
        if(day==0){
            FOR(i,1,D)
            if(Day[1]>=T.A[i]&&Day[1]<=T.B[i])cal(day+1,T.C[i],p,sum);
        }
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]){
                if(day>2)cal(day+1,T.C[i],p,sum+abs(pp-p));
                else cal(day+1,T.C[i],p,sum);
            }
        }
    }
    return res;
}


int main(){
    cin>>N>>D;
    FOR(i,1,N)cin>>Day[i];
    FOR(i,1,D)
        cin>>T.A[i]>>T.B[i]>>T.C[i];
    cout<<cal(0,0,0,0)<<endl;
}