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[150],B[150],C[150];
};

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 mem[day][p][pp]=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 "bits/stdc++.h"

using namespace std;

int main(void){
	int dp[210][210],a[210],b[210],c[210],t[210],d,n,i,j,k,ans=0;
	fill(dp[0],dp[210],0);
	cin >> d >> n;
	for(i=0; i<d; i++){
		cin >> t[i];
	}
	for(j=0; j<n; j++){
		cin >> a[j] >> b[j] >> c[j];
	}
	for(k=1; k<d; k++){

		for(i=0; i<n; i++){
			if(a[i]<=t[k] && b[i]>=t[k]){

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

			}
		}

	}
	for(i=0; i<n; i++){
		ans = max(ans,dp[d-1][i]);
	}
	cout << ans << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

typedef pair<int,int> P;
typedef pair<P,int> Pi;

int main()
{
  int D,N;
  cin >> D >> N;
  vector<int> T;
  rep(i,D)
    {
      int input;
      cin >> input;
      T.push_back(input);
    }

  vector<Pi> ABC;
  rep(i,N)
    {
      int A,B,C;
      cin >> A >> B >> C;
      ABC.push_back(Pi(P(A,B),C));
    }

  P lim[D+1];
  rep(i,D+1)
    {
      lim[i] = P(10000000,-123);
    }

  for(int i=0;i<D;i++)
    {
      for(int j=0;j<N;j++)
	{
	  if(ABC[j].first.first <= T[i] && T[i] <= ABC[j].first.second)
	    {
	      lim[i].first = min(lim[i].first,ABC[j].second);
	      lim[i].second = max(lim[i].second,ABC[j].second);
	    }
	}
    }

 

  long long int ans = 0;
  for(int i=0;i<D-1;i++)
    {
      ans += max(abs(lim[i].first-lim[i+1].second),abs(lim[i].second-lim[i+1].first));
    }
  cout << ans << endl;



  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int a,b,c,d,n,memo[500][500],a1,a2,m1,m2,hukumm[500];
vector <pair<int,int> > updown;
vector <int> day;
vector <pair<int,int> > huku;
int solve(int dp,int one,int sum);


int main(){

    cin >> d >> n;
    for(int i=0;i<d;i++){
        cin >> a;
        day.push_back(a);
    }
    for(int i=0;i<n;i++){
        cin >> a >> b >> c;
        updown.push_back(make_pair(a,b));
        huku.push_back(make_pair(c,i));
    }

    std::sort(huku.begin(),huku.end());

    for(int i=0;i<n;i++){
            if(updown[huku[i].second].second>=day[0]&&day[0]>=updown[huku[i].second].first){
                m1=solve(1,huku[i].first,0);
                break;
            }
        }
    for(int i=n-1;i>-1;i--){
            if(updown[huku[i].second].second>=day[0]&&day[0]>=updown[huku[i].second].first){
                m2=solve(1,huku[i].first,0);
                break;
            }
        }


    cout << max(m1,m2) << endl;

return 0;
}


int solve (int dp,int one,int sum){

    //cout << "#######" << endl;

    int s1=0,s2=0;
    if(dp==d)return sum;
    else {
        if(memo[dp][one]!=0&&memo[dp][one]==sum)return memo[dp][one];

        for(int i=0;i<n;i++){
            if((updown[huku[i].second].second>=day[dp]&&day[dp]>=updown[huku[i].second].first)||hukumm[dp]==huku[i].first){
                    if(hukumm[dp]==0)hukumm[dp]=huku[i].first;
                int sa=max(one,huku[i].first)-min(one,huku[i].first);
                s1=solve(dp+1,huku[i].first,sum+sa);
                break;
            }
        }
        for(int i=n-1;i>-1;i--){
            if((updown[huku[i].second].second>=day[dp]&&day[dp]>=updown[huku[i].second].first)||hukumm[dp]==huku[i].first){
                    if(hukumm[dp]==0)hukumm[dp]=huku[i].first;
                int sa=max(one,huku[i].first)-min(one,huku[i].first);
                s2=solve(dp+1,huku[i].first,sum+sa);
                break;
            }
        }
        return memo[dp][one]=max(s1,s2);
    }

}

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

int D, N;
int T[202];
int A[202], B[202], C[202];
int dp[202][202]; //[テ、ツスツ陛ヲツ猟・テァツ崢ョ][テ・ツ可催ヲツ猟・テァツ敖?」ツ?淌ヲツ慊江

bool check(int day, int ai){
  if(A[ai] <= T[day] && T[day] <= B[ai]){
    return true;
  }
  return false;
}

void solve(){
  memset(dp, -1, sizeof(dp));

  for(int j = 0; j < N; j++){
    if(check(0, j)){
      dp[0][j] = 0;
    }
  }

  for(int i = 0; i < D - 1; i++){
    for(int j = 0; j < N; j++){
      if(dp[i][j] == -1) continue;

      for(int k = 0; k < N; k++){
        if(!check(i + 1, k)) continue;
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(C[j] - C[k]));
      }
    }
  }

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

int main(){
  while(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];
    }
    solve();
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <cstring>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int D, N;
	cin >> D >> N;
	int T[D];
	rep(i, 0, D) cin >> T[i];
	int A[N], B[N], C[N];
	rep(i, 0, N) cin >> A[i] >> B[i] >> C[i];

	int dp[D + 1][N + 1];
	memset(dp, 0, sizeof(dp));


	rep(i, 1, D) {
		rep(j, 0, N) {
			rep(k, 0, N) {
				if(A[j] <= T[i] && T[i] <= B[j] && A[k] <= T[i - 1] && T[i - 1] <= B[k]) {
					dp[i + 1][j] = max(dp[i][k] + abs(C[k] - C[j]), dp[i + 1][j]);
				}
			}
		}
	}

	int ans = 0;
	rep(i, 0, N) {
		ans = max(ans, dp[D][i]);
	}

	cout << ans << endl;

}