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 <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(n+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>
using namespace std;
int dp[250][150],T[250],x[250][3],D,N,MAX;
main(){memset(dp,-1,sizeof(dp));
    cin>>D>>N;
    for(int i=1;i<=D;i++){cin>>T[i];}
    for(int i=1;i<=N;i++){
        cin>>x[i][0]>>x[i][1]>>x[i][2];
        if(T[1]>=x[i][0] && T[1]<=x[i][1]){dp[1][x[i][2]]=0;}
    }
    for(int i=2;i<=D;i++){
        for(int j=1;j<=N;j++){
            if(T[i]>=x[j][0] && T[i]<=x[j][1]){
                for(int k=0;k<=100;k++){
                    dp[i][x[j][2]]=max(dp[i][x[j][2]],dp[i-1][k]+abs(x[j][2]-k));
                }
            }
        }
    }
    for(int i=0;i<=100;i++){MAX=max(MAX,dp[D][i]);}
    cout<<MAX<<endl;
    return 0;
}

C++: #include<iostream>

int main(){
	//Cs[i][0]: min, Cs[i][1]: max, Cs[i][2]: 派手さ
	//dp[i][j]: i日目に服jを着た時の派手さの最大値
	int D, N, Ts[200], Cs[200][3], dp[200][200];
	const int INF = 1 << 24;

	std::cin >> D >> N;
	for(int i=0;i<D;i++){
		std::cin >> Ts[i];
	}
	for(int j=0;j<N;j++){
		std::cin >> Cs[j][0] >> Cs[j][1] >> Cs[j][2];
	}

	for(int i=0;i<200;i++){
		for(int j=0;j<200;j++){
			dp[i][j] = 0;
		}
	}

	int res = 0;
	for(int i=1;i<D;i++){
		for(int j=0;j<N;j++){
			if(Cs[j][0] <= Ts[i] && Ts[i] <= Cs[j][1]);
			else continue;
			for(int k=0;k<N;k++){
				if(Cs[k][0] <= Ts[i-1] && Ts[i-1] <= Cs[k][1]);
				else continue;
				dp[i][j] = std::max(dp[i][j], std::abs(Cs[j][2]-Cs[k][2]) + dp[i-1][k]);
				res = std::max(res, dp[i][j]);
			}
		}
	}

	std::cout << res << std::endl;
}

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

int main(){
    int d, n, t[201], a[201], b[201], c[201], dp[201][201] = {}, ans = 0;
    bool can[201][201];

    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 <= d; i++){
        for(int j = 1; j <= n; j++){
            if(a[j] <= t[i] && t[i] <= b[j]){
                can[i][j] = 1;
            }
        }
    }


    for(int i = 1; i <= d; i++){
        for(int j = 1; j <= n; j++){
            if(can[i][j]){
                for(int l = 1; l <= n; l++){
                    if(can[i - 1][l]){
                        int x = ((c[j] > c[l])? c[j] - c[l]: c[l] - c[j]);
                        dp[i][j] = max(dp[i][j], x + dp[i - 1][l]);
                    }
                }
            }
            if(i == d){
                ans = max(ans, dp[i][j]);
            }
        }
    }

    cout << ans << endl;

    return 0;
}

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[MAX_N],B[MAX_N],C[MAX_N];
};
 
int N,D;
LL res=-INF;
LL Day[MAX_N];
LL mem[250][150];
clothes T;
 
LL cal(LL day,LL p,LL pp,LL sum){
    //cout<<sum+abs(pp-p)<<" "<<day<<" "<<p<<" "<<pp<<endl;
    if(!mem[day][p])res=mem[day][p];
    if(res<sum+abs(pp-p))res=sum+abs(pp-p);
    if(day==N+1)return 0;
    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 mem[day][p]=res;
}
 
 
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];
    }
        cal(0,0,0,0);
        cout<<res<<endl;
}

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

int t[205];
int d;
int n;
int garment[205][3];
int dp[205][205];
int ans;

int dfs(int, int);

int main()
{
  for(int i = 0; i < 205; i++)
  {
    for(int j = 0; j < 65; j++)
      dp[i][j] = -1;
  }

  cin >> d >> n;
  for(int i = 0; i < d; i++)
    cin >> t[i];
  for(int i = 0; i < n; i++)
    cin >> garment[i][1] >> garment[i][2] >> garment[i][0];

  for(int i = 0; i < n; i++)
  {
    if(t[0] >= garment[i][1] && t[0] <= garment[i][2])
    {
      ans = max(dfs(i, 1), ans);
    }
  }
  cout << ans << endl;
}

int dfs(int last, int day)
{
  //cout << " -- day" << day + 1 << " t" << t[day] << endl; 
  if(dp[last][day] != -1)
  {
    //cout << "dp" << endl;
    return dp[last][day];
  }  
  if(day == d)
  {
    //cout << "return " << day + 1 << endl;
    return 0;
  }

  int r = 0;
  for(int i = 0; i < n; i++)
  {
    //cout << garment[i][1] << " - " << garment[i][2] << ": " << garment[i][0]; 
    if(t[day] >= garment[i][1] && t[day] <= garment[i][2])
    {  
      //cout << "OK\n";
      r = max(dfs(i, day + 1) + abs(garment[i][0] - garment[last][0]), r);
      //cout << "----" << endl;
    }
    //else
      //cout << "NG\n";
  }
  dp[last][day] = r;
  return r;
}