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 <algorithm>
using namespace std;

int ab(int A,int B){
	A-=B;
	if(A<0)A*=(-1);
	return A;
}

int main(){
	int d,n;//2<=d<=200//1<=n<=200
	int hot[200];
	int clo[200][3];//最低/最高/派手さ
	cin>>d>>n;
	for(int i=0;i<d;i++)cin>>hot[i];
	for(int i=0;i<n;i++)for(int j=0;j<3;j++)cin>>clo[i][j];
	int dp[201][201]={0};//day//clo
	for(int i=0;i<n;i++)if(!(clo[i][0]<=hot[0]&&hot[0]<=clo[i][1]))dp[0][i]=-1;
	for(int i=1;i<d;i++){
		for(int j=0;j<n;j++){
			if(clo[j][0]<=hot[i]&&hot[i]<=clo[j][1]){
				for(int k=0;k<n;k++){
					if(dp[i-1][k]>=0){
						dp[i][j]=max(dp[i][j],dp[i-1][k]+ab(clo[j][2],clo[k][2]));
					}
				}
			}
			else dp[i][j]=-1;
		}
	}
	int ans=0;
	for(int i=0;i<n;i++)ans=max(ans,dp[d-1][i]);
	cout<<ans<<endl;
	
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

int main(){
    int d, n; cin>>d>>n;
    vector<int> t(d);
    for(int i=0; i<d; i++) cin>>t[i];
    vector<int> a(n), b(n), c(n);
    for(int i=0; i<n; i++) cin>>a[i]>>b[i]>>c[i];

    vector<vector<int>> dp(d+10, vector<int>(110, 0));

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

    int ans = 0;
    for(int i=0; i<=100; i++){
        ans = max(ans, dp[d][i]);
    }

    cout << ans << endl;
}

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

using namespace std;

struct clothes{
    LL A[100];
    LL B[100];
    LL C[100];
};
static const LL INF = 1<<25;
int N,D;
LL ans=-INF;
LL Day[100];
clothes  T;

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


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(1,0,0)<<endl;
}

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

using namespace std;

const int D = 200, N = 200;

int temp[D];
int mint[N], maxt[N], c[N];
int a[D][N] = { { 0 } };

int main(){
	int d, n, r = 0;
	cin >> d >> n;
	for(int i = 0;i < d;++i){
		cin >> temp[i];
	}
	for(int i = 0;i < n;++i){
		cin >> mint[i] >> maxt[i] >> c[i];
	}
	for(int i = 1;i < d;++i){
		for(int j = 0;j < n;++j){
			if(mint[j] <= temp[i] && temp[i] <= maxt[j]){
				for(int k = 0; k < n;++k){
					if(mint[k] <= temp[i - 1] && temp[i - 1] <= maxt[k]){
						a[i][j] = max(a[i][j], a[i - 1][k] + abs(c[k] - c[j]));
					}
				}
			}
		}
	}
	for(int i = 0;i < n;++i){
		r = max(r, a[d - 1][i]);
	}
	cout << r << endl;
	return 0;
}

C++: #include<cstdio>
#include<queue>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<map>
#include<cmath>

using namespace std;

int d, n,t[200],a[200],b[200],c[200];
int dp[200][200] = {};

int ans(int day, int cloth){
	if (day == d-1)return 0;
	if (dp[day][cloth] > 0)return dp[day][cloth];
	int ret = 0;
	for (int i = 0; i < n; i++){
		if (a[i] <= t[day+1] && b[i] >= t[day+1]){
			ret = max(ret, ans(day + 1, i) + abs(c[cloth] - c[i]));
		}
	}
	return dp[day][cloth]=ret;
}

int main(){
	int answer = 0;
	scanf("%d %d", &d, &n);
	for (int i = 0; i < d; i++){
		scanf("%d", &t[i]);
	}
	for (int i = 0; i < n; i++){
		scanf("%d %d %d",&a[i],&b[i],&c[i]);
	}
	for (int i = 0; i < n; i++){
		if (a[i] <= t[0] && b[i] >= t[0]){
			answer = max(answer, ans(0, i));
		}
	}
	printf("%d\n",answer);
	return 0;
}