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 <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct cl{
	int A, B, C;
};
int max( int p, int q ) {
	if( p > q ) return p;
	return q;
}
int abs( int a ) {
	if( a < 0 ) return -a;
	return a;
}

int main(void){

	int D, N;
	cin >> D >> N;
	vector< int > temp( D, 0 );
	vector< cl > cloth( N );

	// dp[日にち][今日着る服のID] = 派手さの絶対値の和の最大値
	vector< vector< int > > dp =
		( vector< vector< int > > ( D,  vector< int >( N, 0 ) ) );

	//データ入力
	for( int i = 0; i < D; i++ ) {
		cin >> temp[ i ];
	}
	for( int i = 0; i < N; i++ ) {
		cin >> cloth[i].A >> cloth[i].B >> cloth[i].C;
	}

	//初期化
	for( int i = 0; i < N; i++ ) {
		if( !( cloth[ i ].A <= temp[ 0 ] && temp[ 0 ] <= cloth[ i ].B ) ) {
			dp[ 0 ][ i ] = -1;
		}
	}

	for( int d = 1; d < D; d++ ) {
		for( int i = 0; i < N; i++ ) {
			if( dp[ d - 1 ][ i ] != -1 ) {
				for( int j = 0; j < N; j++ ) {
					if( cloth[j].A <= temp[d] && temp[d] <= cloth[j].B ) {
						dp[ d ][ j ] = max( dp[ d ][ j ], dp[ d - 1 ][ i ] + abs( cloth[ i ].C - cloth[ j ].C ) );
					} else {
						dp[ d ][ j ] = -1;
					}
				}
			}
		}
	}

	int ans = 0;
	for( int i = 0; i < N; i++ ) {
		ans = max( ans, dp[ D - 1 ][ i ] );
	}

	cout << ans << endl;

	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define X real()
#define Y imag()
#define value(x,y,w,h) (x >= 0 && x < w && y >= 0 && y < h)
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

typedef long long ll ;
typedef deque<int> di ;
typedef deque<ll> dl ;
typedef map<string,int> dict;
typedef complex<double> comd;
typedef pair<int,int> pii;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

double CalcDist(comd p1, comd p2){
  return sqrt(pow(p1.X - p2.X,2.0) + pow(p1.Y -p2.Y,2.0));
}

template <typename T>
void out(deque < T > d)
{
  for(size_t i = 0; i < d.size(); i++)
  {
    debug(d[i]);
  }
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N,D;
  cin >> N >> D;
  vector<int> d(N),a(D),b(D),c(D);
  vector<int> day[N];
  rep(i,N){
    cin >> d[i];
  }
  rep(i,D){
    cin >> a[i] >> b[i] >> c[i];
  }
  rep(j,N){
    int u = 0;
    rep(i,D){
      if(a[i] <= d[j] && d[j] <= b[i]){
        ++u;
        day[j].pb(c[i]);
      }
    }
    sort(all(day[j]));
  }
  rep(j,N){
    rep(i,day[j].size()){
      cout << day[j][i] << " " ;
    }
    cout << endl;
  }
  int ans = 0;
  int dp[2] = {};
  REP(i,0,N-1){
    int lm = 0,hm = 0;
    lm = abs(day[i][0]-day[i+1][0]);
    if(lm < abs(day[i][0]-day[i+1][day[i+1].size()-1])){
      lm = max(lm,abs(day[i][0]-day[i+1][day[i+1].size()-1])) ;
      dp[1] += lm;
    }
    else{
      dp[0] += lm;
    }
    if(day[i].size() != 1){
      hm = abs(day[i][day[i].size()-1]-day[i+1][0]);
      if(hm < abs(day[i][day[i].size()-1]-day[i+1][day[i+1].size()-1])){
        hm = max(hm,abs(day[i][day[i].size()-1]-day[i+1][day[i+1].size()-1])) ;
        dp[1] += hm;
      }
      else{
        dp[0] += hm;
      }
    }
//     debug(dp[0]);
//     debug(dp[1]);
  }
  cout << max(dp[0],dp[1]) << endl;
  return 0;
}

C++: #include <iostream>
#include <string.h>
using namespace std;

int memo[500][500];
int D, N, T[500], A[500], B[500], C[500]; 

int rec(int d, int c){
	if(d == D)
		return 0;
	if(memo[d][c] != -1)
		return memo[d][c];
	
	int res = 0;	
	for(int i = 0; i < N; ++i){
		if(A[i] <= T[d] && T[d] <= B[i])
			res = max(res, rec(d + 1, C[i]) + abs(c - C[i]));
	}
	return memo[d][c] = res;
}

int main() {
	memset(memo, -1, sizeof(memo));
	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];
	
	int ans = 0;
	for(int i = 0; i < N; ++i){
		if(A[i] <= T[0] && T[0] <= B[i])
			ans = max(ans, rec(1, C[i]));
	}
	cout << ans << endl;
	return 0;
}

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

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

int d, n;
int t[202];
vector<uyu> po;

int dp[202][101];

int dfs(int i, int cost, int front)
{
  int &ret = dp[i][front];
  
  if (ret != -1) return ret;
  if (i == d) return cost;
  if (i == 0){
    for (auto &j : po){
      if (t[i] < j.a || j.b < t[i]) continue;
      ret = max(ret, dfs(i + 1, 0, j.c));
    }
    return ret;
  }

  for (auto &j :po){
    if (t[i] < j.a || j.b < t[i]) continue;
    ret = max(ret, dfs(i + 1, cost + abs(front - j.c), j.c));
  }

  return ret;
}

int main()
{
  memset(dp, -1, sizeof(dp));
  cin >> d >> n;
  for (int i = 0; i < d; i++){
    cin >> t[i];
  }

  for (int i = 0; i < n; i++){
    uyu s;
    cin >> s.a >> s.b >> s.c;
    po.push_back(s);
  }

  cout << dfs(0, 0, 0) << endl;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
	int d, n;
	cin >> d >> n;

	vector<int> t(d);
	rep(i, d) cin >> t[i];

	vector<int> a(n), b(n), c(n);
	rep(i, n) cin >> a[i] >> b[i] >> c[i];

	int dp[205][205];
	rep(i, 205) rep(j, 205) dp[i][j] = -1;

	rep(i, n) {
		if(a[i] <= t[0] && t[0] <= b[i]) dp[0][i] = 0;
	}

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

	// rep(i, d + 1) {
	// 	rep(j, n) cout << dp[i][j] << " ";
	// 	cout << endl;
	// }

	int ans = 0;
	rep(i, n) {
		ans = max(ans, dp[d-1][i]);
	}

	cout << ans << endl;


	return 0;
}