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<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

struct cloth{int a,b,c;};
const int MAX = 205;
vector<cloth> V;
vector<int> Temp;
int N,D,dp[MAX][MAX];

void input(){
  cin >> D >> N;
  Temp.resize(D);
  V.resize(N+1);
  for(int i = 0; i < D; i++) cin >> Temp[i];
  for(int i = 0; i < N; i++) cin >> V[i].a >> V[i].b >> V[i].c;
}

bool isValid(int temp, cloth c){
  return c.a <= temp && temp <= c.b;
}

void solve(){
  memset(dp,-1,sizeof(dp));

  for(int i = 0; i < V.size(); i++){
    if(isValid(Temp[0],V[i])) dp[0][i] = 0;
  }

  for(int i = 0; i < D-1; i++){
    for(int j = 0; j < V.size(); j++)
      for(int k = 0; k < V.size(); k++){
	if(dp[i][j] == -1) continue;
	if(!isValid(Temp[i+1],V[k])) continue;
	int diff = abs(V[k].c - V[j].c);
	dp[i+1][k] = max(dp[i+1][k],dp[i][j]+diff);
      }
  }
  int ans = 0;

  for(int i = 0; i < V.size(); i++) ans = max(ans,dp[D-1][i]);
  cout << ans << endl;
}

int main(){
  input();
  solve();
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

const int INF = 1e+8;
const int MAX_D = 201;
const int MAX_N = 201;

int D, N, T[MAX_D], A[MAX_N], B[MAX_N], C[MAX_N];

int main(){
	// 入力
	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];
	
	// dp[d][k] := d日目で前の日に服kを着た時の派手さの差の絶対値の合計
	int dp[MAX_D][MAX_N];
	for(int d=0 ; d < D ; d++ ) fill(dp[d], dp[d] + MAX_N, -1);
	for(int i=0 ; i < N ; i++ ){
		if( A[i] <= T[0] && T[0] <= B[i] ){
			dp[0][i] = 0;
		}
	}
	
	for(int d=0 ; d < D-1 ; d++ ){
		for(int i=0 ; i < N ; i++ ){
			if( dp[d][i] == -1 ) continue;
			
			for(int j=0 ; j < N ; j++ ){
				if( A[j] <= T[d+1] && T[d+1] <= B[j] ){
					dp[d+1][j] = max(dp[d+1][j], dp[d][i] + abs(C[i] - C[j]));
				}
			}
		}
	}
	int ans = 0;
	for(int i=0 ; i < N ; i++ ){
		ans = max(ans, dp[D-1][i]);
	}
	cout << ans << endl;
}

C++: #include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
static const LL INF = 1<<24;
static const int MAX_N = 300;
using namespace std;

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

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

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

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

typedef struct {
  int a;
  int b;
  int c;
} data;

int d, n, ans = 0;
int maxmin[2][65] = {0};
int t[205] = {0};
data ls[205];
// j日にiの服を着るときのそれまでの最大値
int dp[2][205] = {0};

void solve();

int main() {
  int i, j;
  cin >> d >> n;
  for(i = 0; i < d; ++i) cin >> t[i];
  for(i = 0; i < 65; ++i) maxmin[1][i] = inf;
  //その温度のときの派手さの最大値と最小値も記録する。
  for(i = 0; i < n; ++i) {
    cin >> ls[i].a >> ls[i].b >> ls[i].c;
    for(j = ls[i].a; j <= ls[i].b; ++j) {
      maxmin[0][j] = max(maxmin[0][j], ls[i].c);
      maxmin[1][j] = min(maxmin[1][j], ls[i].c);
    }
  }
  solve();
  cout << max(dp[0][d - 1], dp[1][d - 1]) << endl;
  return 0;
}

void solve() {
  int i, j, k;
  for(i = 1; i < d; ++i) {
    for(j = 0; j < 2; ++j) {
      for(k = 0; k < 2; ++k) {
        int bf = maxmin[k][t[i - 1]], now = maxmin[j][t[i]];
        now = max(now - bf, bf - now);
        dp[j][i] = max(dp[j][i], dp[k][i - 1] + now);
      }
    }
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <string>
#include <map>
using namespace std;
#define FOR(I,N) for(int I = 0; I < (int)(N); I++)
#define fin(V) cout<< V <<endl
#define INF 114514
string sValueOf(int v){stringstream ss;ss<<v;return ss.str();}
int parseInt(string v){int i;stringstream ss(v);ss>>i;return i;}

int D, N;
int day[101];
int MIN[201], MAX[201], rare[201];
int dp[201][101];
int mx = 0;
int solve(int day, int now, int bef);


int solve(int n, int b){
	if(n == D)
		return 0;
	if(dp[n][b] != -1){
		return dp[n][b];
	}

	int mx = 0;
	for(int i = 0; i < N; i++){
		if(MAX[i] >= day[n] && MIN[i] <= day[n]){
			mx = max(mx, abs(rare[i] - b) + solve(n+1, rare[i]));
		}
	}
	return dp[n][b] = mx;
}


int main(){
	fill_n(*dp, 201*101, -1);
	cin >> D >> N;
	for(int i = 0; i < D; i++){
		cin >> day[i];
	}
	for(int i = 0; i < N; i++){
		cin >> MIN[i] >> MAX[i] >> rare[i];
	}

	int mx = 0;
	//一番最初の服を決める
	for(int i = 0; i < N; i++){
		if(MAX[i] >= day[0] && MIN[i] <= day[0]){
			mx = max(mx, solve(1, rare[i]));
		}
	}

	fin(mx);
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF -9999999

using namespace std;

int main ()
{
	int d, n, a[205], b[205], c[205], t[205], dp[205][205], ans=0;
	memset(dp, 0, sizeof(dp));
	cin>>d>>n;
	for (int i=1; i<=d; i++) cin>>t[i];
	for (int i=1; i<=n; i++) cin>>a[i]>>b[i]>>c[i];
    for (int i=1; i<=n; i++) if (t[1]<a[i] || t[1]>b[i]) dp[1][i]=INF;
    for (int i=2; i<=d; i++)
    {
        for (int j=1; j<=n; j++)
        {
            if (t[i]<a[j] || t[i]>b[j]) dp[i][j]=INF;
            else
            {
                for (int k=1; k<=n; k++)
                {
                    dp[i][j]=max(dp[i][j], dp[i-1][k]+abs(c[j]-c[k]));
                }
            }
        }
    }
    for (int i=1; i<=n; i++) ans=max(ans, dp[d][i]);
    cout<<ans<<'\n';
}