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<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
	int a[200][200];
	int i2,i,n, m,o[300], x[300], y[300], z[300],i3;
	scanf("%d %d", &n, &m);
	for (i = 1; i <= n; i++)
		scanf("%d", &o[i]);
	for (i = 1; i <= m; i++)
		scanf("%d %d %d", &x[i], &y[i], &z[i]);
	for (i = 0; i <= n; i++)
	for (i2 = 0; i2 <= m; i2++)
		a[i][i2] = -1;
	for (i = 1; i <= m; i++){
		if (o[1] >= x[i] && o[1] <= y[i]){
			a[1][i] = 0;
		}	
	}

	for (i = 1; i <= n; i++){
		for (i2 = 1; i2 <= m; i2++){
			if (a[i][i2] >= 0){
				for (i3 = 1; i3 <= m; i3++){
					if (o[i + 1] >= x[i3] && o[i + 1] <= y[i3])
						a[i + 1][i3] = max(a[i][i2] + abs(z[i2] - z[i3]), a[i + 1][i3]);


				}
			}
		}
	}
		/*for (i = 1; i <= n; i++){
			for (i2 = 1; i2 <= m; i2++){
				printf("%3d", a[i][i2]);
			}
			printf("\n");
		}*/
		int ma = 0;
		for (i = 1; i <= m; i++){
			if (a[n][i] > ma)
				ma = a[n][i];
		}
		printf("%d\n", ma);
	
}

C++: #include<iostream>

using namespace std;

int d,n;
int tenki[202];
int saiko[101],saitei[101];
int hade[101];
int dp[201][101];

int hot(int d,int f){
	if(dp[d][f]!=0)return dp[d][f];
	if(d==0){
		cout<<d<<"a "<<f<<" "<<endl;
		return dp[d][f]=0;
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(saitei[i]<=tenki[d]&&tenki[d]<=saiko[i]){
			ans=max(ans,hot(d-1,i)+abs(hade[i]-hade[f]));
		}
		cout<<d<<"asi "<<f<<" "<<ans<<endl;
	}
	cout<<d<<"as "<<f<<" "<<ans<<endl;
	return dp[d][f]=ans;
}

int main(){
	cin>>d>>n;
	for(int i=1;i<=d;i++){
		cin>>tenki[i];
	}
	for(int i=1;i<=n;i++){
		cin>>saitei[i]>>saiko[i]>>hade[i];
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(saitei[i]<=tenki[d-1]&&tenki[d-1]<=saiko[i]){
			ans=max(ans,hot(d-1,i));
		}
	}
	cout<<ans<<endl;
	return 0;
}

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

struct clothes{
    int a;
    int b;
    int c;
};

int main(){
    int d,n;
    cin >> d >> n;
    
    int D[201];
    clothes N[201];
    int B[201][201];
    for(int i=1; i<=d; i++){
        for(int j=1; j<=n; j++){
            B[i][j] = -1;
        }
    }
    for(int i=1; i<=d; i++){
        cin >> D[i];
    }
    for(int i=1; i<=n; i++){
        cin >> N[i].a >> N[i].b >> N[i].c;
    }
    int y=0,w=0,s=0;
    for(int i=1; i<=d; i++){
        y++; w=0;
        for(int j=1; j<=n; j++){
            if(D[i]>=N[j].a && D[i]<=N[j].b){
                w++;
                s++;
                B[y][w] = N[j].c;
                /*cout <<  "B[" << y << "]" << "[" << w << "] = "
                << B[y][w] << endl;*/
            }
        }
    }
    int T[201];
    for(int i=1; i<=d; i++){
        T[i] = 0;
    }
    for(int i=1; i<=d-1; i++){
        for(int j=1; B[i][j]!=-1; j++){
            for(int r=1; B[i+1][r]!=-1; r++){
                T[i] = max(T[i], abs(B[i+1][r]-B[i][j]));
            }
        }
    }
    int sum=0;
    for(int i=1; i<=d; i++){
        sum += T[i];
    }
    cout << sum << endl;
}

C++: //yukicoder@cpp14
//author:luckYrat(twitter:@luckYrat_)
//<ここに一言>

//せんげん！
#include <iostream>

#include <cmath>
#include <algorithm>
#include <iomanip>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <bitset>

#include <cctype>
#include <utility>
#include <climits>


//なまえくーかん！
using namespace std;
using ll = long long;
using P = pair<ll,ll>;

//てーすう！
const int mod = 1000000007;
const int inf = (1<<30)-1;
const ll linf = (1LL<<62LL)-1;
const double EPS = (1e-10);

//でふぁいん！
#define anyfill(n,s) setw(n) << setfill(s)
#define loop(s) for(int i = 0; s > i; i++)
#define rep(i,q) for(int i = 0; (q) > i; i++)
#define repp(i,n,q) for(int i = n; (q) > i; i++)
#define dep(i,q) for(int i = (q); 0 < i; i--)

//みじかく！
#define pb push_back
#define fir first
#define scn second
#define ednl endl

//いぇすのー！
#define YesNo(a) (a?"Yes":"No")
#define YESNO(a) (a?"YES":"NO")
#define yesno(a) (a?"yes":"no")

//きんぼーnほーこー！！
P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
template <typename T>
T gcd(T a,T b){
  if(a%b==0)return b;
  else return gcd(b,a%b);
}
template <typename T>
T lcm(T a,T b){
  return a/gcd(a,b)*b;
}
int dp[200][200];
struct z{
	int a,b,c;
};
__attribute__((constructor))
void initial() {
 cin.tie(0);
 ios::sync_with_stdio(false);
}
int main(){
	int d,n;cin>>d>>n;
	int p[d];
	for(int i = 0; d > i; i++){
		cin>>p[i];
	}
	z o[n];
	for(int i = 0; n > i; i++){
		cin>>o[i].a>>o[i].b>>o[i].c;
	}
	for(int i = 0; n > i; i++){
		if(o[i].a <= p[0] && p[0] <= o[i].b)dp[0][i] = 0;
		else dp[0][i]=-1;
	}
	for(int i = 1; d > i; i++){
		for(int j = 0; n > j; j++){
			int mx = -1;
			if(o[j].a <= p[i] && p[i] <= o[j].b){
				for(int k = 0; n > k; k++){
					if(dp[i-1][k]==-1)continue;
					mx = max(mx,dp[i-1][k]+abs(o[k].c-o[j].c));
				}
				dp[i][j]=mx;
			}else{
				dp[i][j]=-1;
			}
		}
	}
	int ans = 0;
	for(int i = 0; n > i; i++){
		ans = max(ans,dp[d-1][i]);
	}
	cout << ans << endl;
}

C++: #include<cstdio>
using namespace std;
int sai[1001][1001],hy[1001][1001],t[1001],a[1001],b[1001],c[1001];
int main(void)
{
	int i,j,d,n,sa=0,mm=-1,k;
	scanf("%d %d",&d,&n);
	for(i=1;i<=d;i++){
		scanf("%d",&t[i]);
	}
	for(i=1;i<=n;i++){
		scanf("%d %d %d",&a[i],&b[i],&c[i]);
	}
	for(i=1;i<=d;i++){
		for(j=1;j<=n;j++){
			hy[i][j]=0;
		}
	}
	for(i=1;i<=d;i++){
		for(j=1;j<=n;j++){
			if(a[j]<=t[i] && t[i]<=b[j]){
				for(k=1;k<=n;k++){
					if(a[k]<=t[i+1] && t[i+1]<=b[k]){
						sa=c[j]-c[k];
						if(sa<=0) sa=sa*-1;
						if(hy[i+1][k]<sa+hy[i][j])hy[i+1][k]=sa+hy[i][j];
					}
				}
			}
		}
	}
	mm=-1;
	for(i=1;i<=n;i++){
		if(mm<hy[d][i]){
			mm=hy[d][i];
		}
	}
	printf("%d\n",mm);
	return 0;
}