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>
#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][160][160];
LL s_mem[1000000];
clothes T;
int i;

LL cal(LL day,LL p,LL pp,LL sum){
    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(mem[day][p][pp]!=0&&mem[day][p][pp]>=sum+abs(pp-p)){
        res=mem[day][p][pp];
    }
    else{
        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;
    fill(s_mem,s_mem,-1);
    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;
}

C++: #include <iostream>
#include <fstream>
#include <math.h>

using namespace std;
int T[200], A[200], B[200], C[200], D, N;
int Cs[200][200], CsNumber[200], valueCs[200][200];

inline void readinput(){
//	ifstream fin("input_2012_4.dat");
	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];
}

inline void output_2d(int X[200][200]){
	for(int i=0; i<D; i++){
		for(int j=0; j<N; j++){
			cout << X[i][j];
			if(j != N-1) cout << " ";
			else cout << endl;
		}
	}
}

inline void putCs(){
	int idx;
	for(int i=0; i<D; i++){
		idx = 0;
		for(int j=0; j<N; j++){
			if(A[j]<=T[i] && T[i]<=B[j]){
				Cs[i][idx] = C[j];
				idx += 1;
			}
		CsNumber[i] = idx;
		}
	}
}

inline int solver(){
	int max, maxidx, buf;
	
	//initialize
	for(int i=0; i<200; i++){
		for(int j=0; j<200; j++) valueCs[i][j] = 0;
	}
	
	for(int i=D-1; i>0; i--){	// D-1 to 1
		for(int j1=0; j1<CsNumber[i-1]; j1++){
			max = 0;
			for(int j2=0; j2<CsNumber[i]; j2++){
				buf = fabs(Cs[i-1][j1] - Cs[i][j2]) + valueCs[i][j2];
				if(max < buf){
					max = buf;
					maxidx = j2;
				}
			}
			valueCs[i-1][j1] += max;
		}
	}
	max = 0;
	for (int j=0; j<CsNumber[0]; j++){
		buf = valueCs[0][j];
		if(max < buf) max = buf;
	}
	return max;
}

int main(){
	readinput();
	putCs();
	cout << solver();
//	cout << "--- Cs ---" << endl;
//	output_2d(Cs);
//	cout << "--- valueCs ---" << endl;
//	output_2d(valueCs);
	return 0;
}

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

#define INT32_MAX 2147483647

using namespace std;

struct cloth {
    int min,max,c;

    bool operator<(const cloth& right) const {
        return c < right.c;
    }
};


int t[201];
cloth clothes[201];

inline bool isrange(int temp, cloth *c) {
    return (c->min <= temp) && (temp <= c->max);
}

int main() {
    int d,n;
    cin >> d >> n;
    for (int i = 0; i < d; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> clothes[i].min >> clothes[i].max >> clothes[i].c;
    }
    sort(clothes, clothes + n);

    int result = 0;
    int prec;
    for (int j = 0; j < n; ++j) {
        if (!isrange(t[0], &clothes[j])) continue;

        prec = clothes[j].c;
        break;
    }

    for (int i = 1; i < d; ++i) {
        int todaytemp;
        int tempc;
        for (int j = 0; j < n; ++j) {
            if (!isrange(t[i], &clothes[j])) continue;

            todaytemp = abs(prec - clothes[j].c);
            tempc = clothes[j].c;
            break;
        }
        for (int j = n - 1; 0 <= j; --j) {
            if (!isrange(t[i], &clothes[j])) continue;

            int temp = abs(prec - clothes[j].c);
            if (temp < todaytemp) {
                result += todaytemp;
                prec = tempc;
            } else {
                result += temp;
                prec = clothes[j].c;
            }
            break;
        }
    }

    cout << result << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
 
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& a){ return is >> a.first >> a.second; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2>& a){ return os << a.first << " "<<a.second; }
template<typename T> istream& operator>>(istream& is, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) is >> vc[i]; return is; }
template<typename T> ostream& operator<<(ostream& os, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) os << vc[i] << endl; return os; }

 
#define ForEach(it,c) for(__typeof (c).begin() it = (c).begin(); it != (c).end(); it++)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s) { sort(ALL(s)); (s).erase( unique( ALL(s)), (s).end());}
#define fr first
#define sc second
 
typedef pair< int , int > Pi;
typedef pair< int , Pi > Pii;
const int INF = 1 << 30;

typedef long long int64;

int main(){
  int D, N;
  cin >> D >> N;
  vector< int > T(D);
  cin >> T;
  vector< int > A(N), B(N), C(N);
  for(int i = 0; i < N; i++){
    cin >> A[i] >> B[i] >> C[i];
  }
  // dp[i][j]: i - 1日目にjの服を着た時の派手さの差の絶対値の最大値
  vector< vector< int > > dp( D, vector< int >( N, -1));
  for(int i = 0; i < N; i++){ // 1日目に着る服
    if(A[i] <= T[0] && T[0] <= B[i]) 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(A[k] <= T[i] && T[i] <= B[k] ) dp[i][k] = max( dp[i][k], dp[i - 1][j] + abs(C[j] - C[k]));
      }
    }
  }
  int ret = 0;
  for(int i = 0; i < N; i++){
    ret = max( ret, dp[D - 1][i]);
  }
  cout << ret << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <utility>
#define INF 100100100100100100
using namespace std;
typedef long long ll;

ll D,N;
ll T[210],A[210],B[210],C[210];
ll dp[2][110];
int main(){
	cin>>D>>N;
	for(ll i=1;i<=D;i++) cin>>T[i];
	for(ll i=1;i<=N;i++) cin>>A[i]>>B[i]>>C[i];

	ll in=0,ou=1;
	for(ll i=0;i<2;i++) fill(dp[i],dp[i]+110,-1);
	for(ll i=1;i<=N;i++){
		if(A[i]<=T[1]&&T[1]<=B[i]){
			dp[in][C[i]]=0;
		}
	}
	for(ll i=2;i<=D;i++){
		fill(dp[ou],dp[ou]+110,-1);
		for(ll j=1;j<=N;j++){
			if(A[j]<=T[i]&&T[i]<=B[j]){
				for(ll k=0;k<=100;k++){
					if(dp[in][k]!=-1){
						dp[ou][C[j]]=max(dp[ou][C[j]],dp[in][k]+abs(C[j]-k));
					}
				}
			}
		}
		swap(in,ou);
	}
	ll res=0;
	for(ll i=0;i<=100;i++){
		res=max(res,dp[in][i]);
	}
	cout<<res<<endl;
	return 0;
}