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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

int D,N;
int T[202];
struct Shirt {
    int A,B;
    int C;
    Shirt(int A,int B,int C) :
        A(A),B(B),C(C){}
    Shirt() {}
};

Shirt shirt[202];
// i日目にj番目の服を着る
int dp[202][202];
int main() {
    cin>>D>>N;
    rep(i,D) cin>>T[i];
    rep(i,N) {
        int A,B,C;
        cin>>A>>B>>C;
        shirt[i] = Shirt(A,B,C);
    }
    rep(i,D) {
        rep(j,N) {
            rep(k,N) {
                if(!(shirt[k].A<=T[i+1] && T[i+1]<=shirt[k].B)) continue;
                if(!(shirt[j].A<=T[i] && T[i]<=shirt[j].B)) continue;
                dp[i+1][k] = max(dp[i+1][k],dp[i][j] + abs(shirt[k].C-shirt[j].C));
            }
        }
    }
    cout<<(*max_element(dp[D-1],dp[D-1]+N))<<endl;
    return 0;
}

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

int d,n,t[201],data[201][2],dp[201][2];

int main(){
	std::cin>>d>>n;
	for(int i=0;i<d;i++)std::cin>>t[i],data[i][1]=1000000;
	for(int i=0;i<n;i++){
		int a,b,c;
		std::cin>>a>>b>>c;
		for(int j=0;j<d;j++){
			if(a<=t[j]&&t[j]<=b)data[j][0]=std::max(data[j][0],c),data[j][1]=std::min(data[j][1],c);
		}
	}
	for(int i=0;i<d-1;i++){
		for(int j=0;j<2;j++){
			dp[i][j]=std::max(dp[i-1][0],dp[i-1][1])+std::max(abs(data[i][j]-data[i+1][0]),abs(data[i][j]-data[i+1][1]));
		}
	}
	std::cout<<std::max(dp[d-2][0],dp[d-2][0])<<std::endl;
	return 0;
}

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

#define for_(i,a,b) for(int i=a;i<b;++i)

int D, N, T[222], A[222], B[222], C[222], dp[222][111];

int main() {
	cin >> D >> N;
	
	for_(i,0,D) cin >> T[i];
	for_(i,0,N) cin >> A[i] >> B[i] >> C[i];
	
	memset(dp, -1, sizeof(dp));
	
	for_(i,0,N) if (A[i] <= T[0] && T[0] <= B[i]) dp[1][C[i]] = 0;
	
	int ans = 0;
	
	for_(i,1,D) for_(j,0,111) {
		if (dp[i][j] >= 0) {
			for_(k,0,N) {
				if (A[k] <= T[i] && T[i] <= B[k]) {
					int c = C[k];
					dp[i + 1][c] = max(dp[i + 1][c], dp[i][j] + abs(j - c));
					ans = max(ans, dp[i + 1][c]);
				}
			}
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef vector<int> Vi;
typedef pair<int,int> Pii;
typedef pair<int,Pii> Pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

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

int main() {
  int d, n;
  int t[201];
  int dp[201][201];
  edge e[201];
  cin >> d >> n;

  for(int i=0; i<d; i++) {
    for(int j=0; j<n; j++) {
      dp[i][j] = -1;
    }
  }

  for(int i=0; i<d; i++) {
    cin >> t[i];
  }

  for(int i=0; i<n; i++) {
    cin >> e[i].a >> e[i].b >> e[i].c;
  }

  for(int i=0; i<n; i++) {
    if( e[i].a <= t[0] && t[0] <= e[i].b ) {
      dp[0][i] = 0;
    }
  }

  for(int i=1; i<d; i++) {
    for(int j=0; j<n; j++) {
      if( dp[i-1][j] == -1 ) continue;
      for(int k=0; k<n; k++) {
	if( e[k].a <= t[i] && t[i] <= e[k].b ) {
	  dp[i][k] = max(dp[i][k], dp[i-1][j]+abs(e[j].c - e[k].c));
	}
      }
    }
  }

  int ans = 0;
  for(int i=0; i<n; i++) {
    ans = max(ans, dp[d-1][i]);
  }
 
  cout << ans << endl;
}

C++: //AOJ 0579: Hot days
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 10000000;
int n, d, ans, c[200][61], save[3], dt[200], plan[200][2], s[200][101];
queue<P> que;
P p1, p2;
void solve(){
	while (true){
		p1 = que.front();
		que.pop();
		p2 = que.front();
		que.pop();
		s[p1.second + 1][plan[p1.second + 1][0]] = max(s[p1.second][p1.first] + abs(plan[p1.second + 1][0] - p1.first), s[p1.second][p2.first] + abs(plan[p1.second + 1][0] - p2.first));
		s[p1.second + 1][plan[p1.second + 1][1]] = max(s[p1.second][p1.first] + abs(plan[p1.second + 1][1] - p1.first), s[p1.second][p2.first]+abs(plan[p1.second + 1][1] - p2.first));
		que.push(P(plan[p1.second + 1][0], p1.second + 1));
		que.push(P(plan[p1.second + 1][1], p1.second + 1));
		if (p1.second == d - 2){
			for (int i = 0; i <= 1; i++)
				ans = max(ans, s[p1.second + 1][plan[p1.second + 1][i]]);
			return;
		}
	}
}
int main(){
	cin >> d >> n;

	for (int i = 0; i < d; i++){
		cin >> dt[i];
	}
	for (int i = 0; i<n; i++){
		for (int j = 0; j<61; j++){
			c[i][j] = -1;
		}
	}
	for (int i = 0; i < 4; i++){
		cin >> save[0] >> save[1] >> save[2];
		for (int j = save[0]; j <= save[1]; j++){
			c[i][j] = save[2];
		}
	}
	for (int i = 0; i<d; i++){
		plan[i][0] = INF;
	}
	for (int i = 0; i < d; i++){
		for (int j = 0; j < n; j++){
			if (c[j][dt[i]] != -1){
				plan[i][0] = min(c[j][dt[i]], plan[i][0]);
				plan[i][1] = max(c[j][dt[i]], plan[i][1]);
			}
		}
	}
	que.push(P(plan[0][0], 0));
	que.push(P(plan[0][1], 0));
	solve();
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;

ll n, d, p = 0;
int t[1000], a[1000], b[1000], c[1000];
vector< pair<int,int> > vpi;

int main(){
	cin >> n >> d;
	REP(i, n){
		cin >> t[i];
	}
	REP(i, d){
		cin >> a[i] >> b[i] >> c[i];
	}
	REP(i, n){
		ll max = -1, min = 10000;
		REP(j, d){
			if (t[i] >= a[j] && t[i] <= b[j]){
				if (max < c[j])max = c[j];
				if (min > c[j])min = c[j];
			}
		}
		vpi.push_back(make_pair(max, min));
	}
	REP(i, vpi.size() - 1){
		int a = abs(vpi[i + 1].first - vpi[i].first);
		a = max(a, abs(vpi[i + 1].second - vpi[i].first));
		a = max(a, abs(vpi[i + 1].first - vpi[i].second));
		a = max(a, abs(vpi[i + 1].second - vpi[i].second));
		p += a;
	}
	cout << p << endl;
}