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<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[150];
LL mem[250][110][110];
clothes T;

LL cal(LL day,LL p,LL pp,LL sum){
    if(res<sum+abs(pp-p))res=sum+abs(pp-p);
    //if(mem[day][p][pp]!=0)return mem[day][p][pp];
    if(day==N+1)return 0;
    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;
    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 <cstring>
#include <cmath>
using namespace std;

int D, N;
int T[256], A[256], B[256], C[256];
int dp[256][256];

int rec(int day, int prev)
{
	if(day == D) return 0;

	
}

int main()
{
	memset(dp, 0, sizeof dp);
	
	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];
	}

	for(int day = 0; day < D-1; day++) {
		for(int now = 0; now < N; now++) {
			for(int next = 0; next < N; next++) {
				if(A[now] <= T[day] && T[day] <= B[now] && A[next] <= T[day+1] && T[day+1] <= B[next]) {
					dp[day+1][next] = max(int(dp[day+1][next]), int(dp[day][now] + abs(C[now] - C[next])));
				}
			}
		}
	}

	int ans = 0;
	for(int i = 0; i < N; i++) {
		ans = max(ans, dp[D-1][i]);
	}

	cout << ans << endl;
}

C++: #include<stdio.h>
#include<cstdio>
#include<algorithm>
#include<functional>
#include<ctype.h>
using namespace std;
int temp;
int d,n;
int t[10000];
int a[10000],b[10000],c[10000];
int dp[10000][10000];
int i,j,k;
int main(void){
	int max=0;
	scanf("%d %d",&d,&n);	//d=見る日数 n=服の種類
	for(i=0;i<d;i++){
		scanf("%d",&t[i]);	//i日目の最高温度
	}
	for(i=0;i<d;i++){
		for(j=0;j<n;j++){
			dp[i][j]=-1;	//派手さ
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d %d",&a[i],&b[i],&c[i]);	//a=最低温度 b=最高温度 c=派手
	}
	for(i=0;i<n;i++){
		if(a[i]<=t[0] && b[i]>=t[0]){
			dp[0][i]=0;
		}
	}
	for(i=1;i<d;i++){
		for(j=0;j<n;j++){
			if(dp[i-1][j]!=-1){
				for(k=0;k<n;k++){
					if(a[k]<=t[i] && b[k]>=t[i]){
						if(dp[i][k]==-1) {
							dp[i][k]=0;
						}
						temp=c[k]-c[j];
						//printf("c[k]%d c[j]%d\n",c[k],c[j]);
						if(temp<0)	temp=0-temp;
						if(max<=temp){
							max=temp;
						}
						//printf("max%d\n",max);
						//printf("dpa%d i=%d k=%d\n",dp[i][k],i,k);
						if(dp[i][k]<=dp[i-1][j]+max){
							dp[i][k]=dp[i-1][j]+max;
						//printf("aa%d %d %d %d\n",dp[i][j],dp[i][k],i,j);
						}
					}
			max=0;
				}
			}
			//printf("\n");
		}
		//printf("\n");
	}
	/*for(i=0;i<d;i++){ //でバグ
		printf("%d\n",t[i]);
		for(j=0;j<n;j++){
			printf("%d ",dp[i][j]);
		}
		printf("\n");
	}*/			
	max=0;
	for(j=0;j<n;j++){
		//printf("%d ",dp[d-1][j]);
		if(dp[d-1][j]>max){
			max=dp[d-1][j];
		}
	}
	//printf("\n");
	printf("%d\n",max);
	return 0;
}

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

int main(){
	int D,N;
	int Kion[210];
	int Fmin[210],Fmax[210],Fhad[210];
	cin>>D>>N;
	for(int i=0;i<D;i++){
		cin>>Kion[i];
	}
	for(int i=0;i<N;i++){
		cin>>Fmin[i]>>Fmax[i]>>Fhad[i];
	}

	int Kekka[200][200];
	int rinji,rin2;
	//1??\???
	for(int i=0;i<N;i++){
		if(Kion[0]>=Fmin[i]&&Kion[0]<=Fmax[i]) Kekka[0][i]=0;
		else Kekka[0][i]=-1;
	}
	//2??\?????\???
	for(int day=1;day<D;day++){
		for(int fuku=0;fuku<N;fuku++){
			if(Kion[day]>=Fmin[fuku]&&Kion[day]<=Fmax[fuku]){
				rinji=-1;rin2=0;
				for(int mf=0;mf<N;mf++){
					if(Kekka[day-1][mf]!=-1&&rinji<=abs(Fhad[mf]-Fhad[fuku])) {rinji=abs(Fhad[mf]-Fhad[fuku]);rin2=Kekka[day-1][mf];}
				}
				Kekka[day][fuku]=rin2+rinji;
			}
			else{
				Kekka[day][fuku]=-1;
			}
		}
	}
	rinji=0;
	for(int i=0;i<N;i++){
		rinji=max(rinji,Kekka[D-1][i]);
	}
	cout<<rinji<<endl;
	return 0;
}

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


int main(void){

  int dp[200][200];
  int N,D;
  int A[200];
  int B[200];
  int C[200];
  int H[200];
  int v=0;
  int max=0;
  int max_id=0;
  int ans=0;

  cin>>D>>N;
  for(int i=0;i<D;i++)cin>>H[i];
  for(int i=0;i<N;i++)cin>>A[i]>>B[i]>>C[i];

  for(int i=0;i<D;i++)
    for(int j=0;j<N;j++)
      if(H[i]<A[j] || H[i] > B[j])
	dp[i][j]=-1;

  for(int i=1;i<D;i++){
    for(int j=0;j<N;j++){
      if(dp[i][j] != -1){
	v=0;
	max=0;
	for(int k=0;k<N;k++){
	  if(dp[i-1][k]==-1)
	    continue;
	  v = C[j] - C[k];
	  if(v<0)v=-v;
	  v += dp[i-1][k];
	  if(max<v){
	    max=v;  
	  }
	}
	dp[i][j]=max;
	    
      }
    }
  }
  
  for(int i=0;i<N;i++){
    if(ans< dp[D-1][i])
      ans=dp[D-1][i];
  }
  cout<<ans<<endl;
  return 0;
}

C++: 
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<numeric>

#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

struct data {
	int mi_t;
	int mx_t;
	int c;
};
int t[210];
int d[210][2];
int dp[210][2];

int main()
{
	int D, N;
	struct data Data[210];
	cin >> D >> N;

	for (int i = 0; i < D; i++) 
		cin >> t[i];

	for (int i = 0; i < N; i++) 
		cin >> Data[i].mi_t >> Data[i].mx_t >> Data[i].c;

	for (int i = 0; i < 210; i++) {
		d[i][0] = -1;
		d[i][1] = 1000000000;
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < D; j++) {
			if (Data[i].mi_t <= t[j] && Data[i].mx_t >= t[j]) {
				d[j][0] = max(d[j][0], Data[i].c);
				d[j][1] = min(d[j][1], Data[i].c);
			}
		}
	}
	for (int i = 1; i < D; i++) {
		for (int j = 0; j < 2; j++) {
			dp[i][j] = max(abs(d[i - 1][0] - d[i][j]) + dp[i - 1][0],
				abs(d[i - 1][1] - d[i][j]) + dp[i - 1][1]);
		}
	}
	cout << max(dp[D-1][0],dp[D-1][1]) << endl;
	return 0;
}