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[MAX_N];
clothes T;

LL cal(LL day,LL p,LL pp,LL sum){
    //cout<<sum+abs(pp-p)<<" "<<day<<" "<<p<<" "<<pp<<endl;
    if(res<sum+abs(pp-p))res=sum+abs(pp-p);
    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>
using namespace std;
int main(){
	int D,N,T[200],A[200][3];
	int dp[200][101];
	cin >> N >> D ;
	for(int i=0;i<N;i++){
		cin >>T[i];
	}
	for(int i=0;i<D;i++){
		for(int j=0;j<3;j++){
			cin >>A[i][j];
		}
	}
	for(int i=0;i<N;i++){
		for(int j=0;j<D;j++){
			dp[i][j]=-9999;
		}
	}
	for(int l=0;l<D;l++){
		if(T[0]>A[l][0] && T[0]<A[l][1] ){
			dp[ 0 ][ A[l][2] ]=0;
			

		}
	}
	for(int i=1;i<N;i++){
		for(int j=0;j<D;j++){
			if(T[i]>=A[j][0] && T[i]<=A[j][1] ){
				for(int l=0;l<=100;l++){
					if(dp[i-1 ][ l ]>=0)
					dp[ i ][ A[j][2] ]=max(dp[ i ][ A[j][2] ],dp[ i-1 ][ l ]+abs(l-A[j][2]));//テ、ツサツ甘」ツ?セテ」ツ?ァテ」ツ?ョテヲツ慊?ゥツォツ佚」ツ??、ツサツ甘・ツ崢?
					

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

	return 0;
}

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

using namespace std;
int wears[201][3];
int weather[201];
int mem[201][3], N, D;

int dfs(int wear, int n)
{
  if(mem[wear][n] != 0)
    return mem[wear][n];
  if(n == D - 1)
    return 0;
  int ans = 0;
  for(int i = 0; i < N; i++)
  {
    if(wears[i][0] <= weather[n + 1] && wears[i][1] >= weather[n + 1])
      ans = max(ans, dfs(i, n + 1) + abs(wears[i][2] - wears[wear][2]));
  }
  mem[wear][n] = ans;

  return ans;
}

int main()
{
  cin >> D >> N;
  for(int i = 0; i < D; i++)
    cin >> weather[i];
  for(int i = 0; i < N; i++)
  {
    for(int j = 0; j < 3; j++)
      cin >> wears[i][j];
    wears[i][2];
  }
  int ans = 0; 
  for(int i = 0; i < N; i++)
  {
    if(wears[i][0] <= weather[0] && wears[i][1] >= weather[0])
      ans = max(ans, dfs(i, 0));
  }
  cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

#define INF 1145141919

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

int main() {
  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=0; i<D; i++) {
    for (int j=0; j<N; j++) {
      dp[i][j] = -INF;
    }
  }
  for (int i=0; i<N; i++) {
    if (T[0] >= A[i] && T[0] <= B[i]) dp[0][i] = 0;
  }
  for (int i=0; i<D; i++) {
    for (int j=0; j<N; j++) {
      for (int k=0; k<N; k++) {
        if (T[i+1] >= A[k] && T[i+1] <= B[k]) {
          dp[i+1][k] = max(dp[i+1][k], dp[i][j] + abs(C[j] - C[k]));
        }
      }
    }
  }
  int ans = 0;
  for (int i=0; i<N; i++) {
    ans = max(ans, dp[D-1][i]);
  }
  cout << ans << "\n";
  return 0;
}

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

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(D, vector<int>(N));
    for (int d = 1; d < D; ++d) {
        for (int c = 0; c < N; ++c) {
            if (T[d] < A[c] || T[d] > B[c]) continue;
            for (int b = 0; b < N; ++b) {
                if (T[d-1] < A[b] || T[d-1] > B[b]) continue;
                dp[d][c] = max(dp[d][c], dp[d-1][b] + abs(C[b] - C[c]));
            }
        }
    }
    int ans = 0;
    for (int c = 0; c < N; ++c)
        ans = max(ans, dp[D-1][c]);
    cout << ans << endl;
}

C++: #include<bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main()
{
  int D, N, T[200], A[200], B[200], C[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];

  vector< int > curr(N, -INF);
  for(int i = 0; i < N; i++) {
    if(A[i] <= T[0] && T[0] <= B[i]) curr[i] = 0;
  }
  for(int i = 1; i < D; i++) {
    vector< int > next(N, -INF);
    for(int j = 0; j < N; j++) {
      for(int k = 0; k < N; k++) {
        if(A[k] <= T[i] && T[i] <= B[k]) {
          next[k] = max(next[k], curr[j] + abs(C[j] - C[k]));
        }
      }
    }
    curr.swap(next);
  }
  cout << *max_element(begin(curr), end(curr)) << endl;
}

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

#define INT32_MAX 2147483647

using namespace std;

struct cloth {
    int min,max,c;

    bool operator<(const cloth& right) const {
        return c < right.c;
    }
};


int t[201];
cloth clothes[201];

inline bool isrange(int temp, cloth *c) {
    return (c->min <= temp) && (temp <= c->max);
}

int main() {
    int d,n;
    cin >> d >> n;
    for (int i = 0; i < d; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> clothes[i].min >> clothes[i].max >> clothes[i].c;
    }
    sort(clothes, clothes + n);

    int result = 0;
    int prec;
    for (int j = 0; j < n; ++j) {
        if (!isrange(t[0], &clothes[j])) continue;

        prec = clothes[j].c;
        break;
    }

    for (int i = 1; i < d; ++i) {
        int todaymax = -1;
        int tempc;
        for (int j = 0; j < n; ++j) {
            if (!isrange(t[i], &clothes[j])) continue;

            int temp = abs(prec - clothes[j].c);
            if (todaymax < temp) {
                todaymax = temp;
                tempc = clothes[j].c;
            }
        }

        result += todaymax;
        prec = tempc;
    }

    cout << result << endl;

    return 0;
}