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 wear[200][3], day[200];
int mem[200][200];
int D, N;
int dp(int d, int b)
{
  if(d == D)
    return 0;
  if(mem[d][b] != -1)
    return mem[d][b];
  int ans = 0;
  for(int i = 0; i < N; i++)
  {
    if(wear[i][0] <= day[d] && wear[i][1] >= day[d])
      ans = max(ans, dp(d + 1, i) + abs(wear[i][2] - wear[b][2]));
  }
  return ans;
}

int main()
{
  for(int i = 0; i < 200; i++)
    for(int j = 0; j < 200; j++)
      mem[i][j] = -1;

  cin >> D >> N;
  for(int i = 0; i < D; i++)
    cin >> day[i];
  for(int i = 0; i < N; i++)
    cin >> wear[i][0] >> wear[i][1] >> wear[i][2];
  int ans = 0;
  for(int i = 0; i < N; i++)
  {
    if(wear[i][0] <= day[0] && wear[i][1] >= day[0])
      ans = max(ans, dp(1, i));
  }
  cout << ans << endl;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

struct Clothes {
	int a, b, c;
};

int main() {
	int dp[201][200] = {};
	int d, n, mt[200];
	Clothes clothes[200];

	cin >> d >> n;
	REP(i, d) scanf("%d", &mt[i]);
	REP(i, n) scanf("%d %d %d", &clothes[i].a, &clothes[i].b, &clothes[i].c);
	
	REP(i, d - 1) {
		REP(j, n) {
			if (clothes[j].a > mt[i] || clothes[j].b < mt[i]) continue;
			REP(k, n) {
				if (clothes[k].a > mt[i+1] || clothes[k].b < mt[i+1]) continue;
				dp[i+1][k] = max(dp[i+1][k], dp[i][j] + abs(clothes[k].c - clothes[j].c));
			}
		}
	}

	int ans = 0;
	REP(i, n) ans = max(ans, dp[d - 1][i]);
	cout << ans << endl;

	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, -1, sizeof(mat))
typedef long long ll;

int main()
{
  int D, N; cin >> D >> N;
  int T[D]; FOR(i,0,D) cin >> T[i];
  int A[N], B[N], C[N]; FOR(i,0,N) cin >> A[i] >> B[i] >> C[i];
  int dp[D][N]; CLR(dp);// ???????????\??°?????????????????????
  FOR(i,0,N) if(T[0] >= A[i] && B[i] >= T[0]) dp[0][i] = 0;
  FOR(i,0,D-1) {
    FOR(j,0,N) {
      // k???????????´???
      if(dp[i][j] == -1) continue;
      FOR(k,0,N) {
        if(T[i+1] < A[k] || B[k] < T[i+1]) continue;
        dp[i+1][k] = max(dp[i+1][k], dp[i][j] + abs(C[j] - C[k]));
      }
    }
  }
  int ans = 0;
  FOR(i,0,N) ans = max(ans, dp[D-1][i]);
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <cmath>
using namespace std;
int dayCanW[200];
int dayM[200][200];
int days,clothes;
int clothD[200][3];
int temple[200];
int Redirector[201][101];
int bestCloth(int Nowday,int yesterday){
  int point=0;
  int pointM=0;
  if(Nowday<days ){
    if(Redirector[Nowday][yesterday]==-1){
    for(int i=0;i<clothes;i++){
      if(temple[Nowday]>=clothD[i][0] && temple[Nowday]<=clothD[i][1]){
	point=bestCloth(Nowday+1,clothD[i][2]);
	if (Nowday!=0){
	  point+=(int)abs((double)clothD[i][2]-yesterday);
	}
	if(point>pointM){
	  pointM=point;
	}
      }
    }

  Redirector[Nowday][yesterday]=pointM;
  }
    else{
    }
  }
  else{
    return 0;
  }

  return  Redirector[Nowday][yesterday];//;//自分の今までの派手さ絶対値の合計（今日ー昨日まで）

}
int main(void){
  for(int i=0;i<201;i++){
    for(int j=0;j<101;j++){
      Redirector[i][j]=-1;
    }
  }
  cin >> days;
  cin >> clothes;
  for(int i=0;i<days;i++){
    cin >> temple[i];
  }
  for(int i=0;i<clothes;i++){
    cin >> clothD[i][0];
    cin >> clothD[i][1];
    cin >> clothD[i][2];
  }
  for(int i=0;i<days;i++){
	dayCanW[i] = 0;
    for(int j=0;j<clothes;j++){
	dayM[i][j] = 0;
    }
  }
 
  cout << bestCloth(0,0) << endl;
  return 0 ;
}

C++: #include<bits/stdc++.h>
using namespace std;
int a[500],b[500],c[500],day[500],d,n,ans[500][500],fans;

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

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

    for(int i=0;i<n;i++)fans=max(fans,ans[d-1][i]);

    cout << fans << endl;

return 0;
}

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

int main(){
	int d,n,t[200],a[200],b[200],c[200];
	int dp[201][200] = {};
	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 i = 1;i <= d;i++){
		for(int j = 0;j < n;j++){
			if(a[j] <= t[i - 1] && t[i - 1] <= b[j]){
				for(int k = 0;k < n;k++){
					if(i >= 2 && a[k] <= t[i - 2] && t[i - 2] <= b[k]) dp[i][j] = max(dp[i][j],dp[i - 1][k] + abs(c[j] - c[k]));
				}
			}
		}
	}
	int ma = 0;
	for(int i = 0;i < n;i++) ma = max(ma,dp[d][i]);
	cout << ma << 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[150],B[150],C[150];
};

int N,D;
LL res=-INF;
LL Day[150];
LL mem[110][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]!=-1)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 mem[day][p][pp]=res;
}


int main(){
    cin>>N>>D;
    fill(mem[0][0],mem[110][0],-1);
    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;
}