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>
using namespace std;

const long long INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
#define rep(i,a,n) for(int i=a;i<n;i++)

int main(){
    int D,N;cin>>D>>N;
    vector<int> T(D),A(N),B(N),C(N);
    rep(i,0,D) cin>>T[i];
    rep(i,0,N) cin>>A[i]>>B[i]>>C[i];
    vector<vector<int>> dp(D+5,vector<int>(N+10,0));
    rep(i,1,D+1){
        rep(j,0,N){
            if(A[j]<=T[i-1]&&T[i-1]<=B[j]){
                rep(k,0,N){
                    if(i>=2&&A[k]<=T[i-2]&&T[i-2]<=B[k]){
                        chmax(dp[i][j],dp[i-1][k]+abs(C[j]-C[k]));
                    }
                    chmax(dp[i][j],dp[i-1][k]);
                }
            }
        }
    }
    int res=0;
    rep(i,0,N) res=max(res,dp[D][i]);
    cout<<res<<endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int main(){
	int d, n; cin >> d >> n;
	int t[d]; rep(i, d) cin >> t[i];
	int a[n], b[n], c[n]; rep(i, n) cin >> a[i] >> b[i] >> c[i];

	int dp[d][n];
	memset(dp, 0, sizeof(dp));

	rep(i, d)rep(j, n){
		if(!i) {
			if(a[j] <= t[i] && t[i] <= b[j]) dp[i][j] = c[j];
			else dp[i][j] = -1;
		} else if(a[j] <= t[i] && t[i] <= b[j]){
			rep(k, n){
				if(dp[i-1][k] > -1){
					int base = (i == 1) ? 0 : dp[i-1][k];
					dp[i][j] = max(dp[i][j], base + abs(c[k] - c[j]));
				}
			}
		} else dp[i][j] = -1;
	}
	cout << *max_element(dp[d-1], dp[d-1]+n) << endl;

	return 0;
}

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

int memo[201][201];
int d, n;
int t[200];
int a[200], b[200], c[200];

int rec(int k, int prev){
	
	if(memo[k][prev] != -1)return memo[k][prev];
	if(k == d)return 0;
	
	int best = 0;
	for(int next = 0; next < n; next++){
		if(t[k] >= a[next] && t[k] <= b[next])
			best = max(best, rec(k + 1, next) + abs(c[next] - c[prev]));
	}
		
	return memo[k][prev] = best;
		
}
int main() {
	for(int i = 0; i <= 200; i++)
		for(int j = 0; j <= 200; j++)
			memo[i][j] = -1;
			
	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];
		
	int ans = 0;
	for(int i = 0; i < n; i++){
		if(t[0] >= a[i] && t[0] <= b[i])
			ans = max(rec(0, i), ans);
	}

	
	cout << ans << endl;
	
	return 0;
}

C++: #include <cstdio>

int d, n;
int t[200];
int tmax[200];
int tmin[200];
int c[200];
int cmax[61] = {0}; //気温がjの時の派手さの最大値
int cmin[61] = {0}; //気温がjの時の派手さの最小値
int pattern[2][200] = {0};
int sum[2] = {0};

int main() {
	scanf("%d %d", &d, &n);
	for(int i = 0; i < d; i++) {
		scanf("%d", &t[i]);
	}
	for(int i = 0; i < n; i++) {
		scanf("%d %d %d", &tmin[i], &tmax[i], c[i]);
	}
	for(int i = 0; i < n; i++) {
		for(int j = tmin[i]; j <= tmax[i]; j++) {
			cmax[j] = (cmax[j] < c[i]) ? c[i] : cmax[j];
			cmin[j] = (cmin[j] > c[i]) ? c[i] : cmin[j];
		}
	}
	for(int i = 0; i < d; i++) {
		if(i % 2 == 0) {
			pattern[0][i] = cmax[t[i]];
			pattern[1][i] = cmin[t[i]];
		} else {
			pattern[1][i] = cmax[t[i]];
			pattern[0][i] = cmin[t[i]];
		}
	}
	int dif;
	for(int i = 0; i < d-1; i++) {
		dif = pattern[0][i] - pattern[0][i+1];
		sum[0] += (dif < 0) ? -dif : dif;
	}
	for(int i = 0; i < d-1; i++) {
		dif = pattern[1][i] - pattern[0][i+1];
		sum[1] += (dif < 0) ? -dif : dif;
	}
	printf("%d\n", (sum[0] < sum[1]) ? sum[1] : sum[0]);
	return 0;
}

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

using namespace std;

typedef struct {
	int a;
	int b;
	int c;
} fuku_t;

int d, n;

int t[200];
fuku_t fuku[200];

int dp[200][200];

int main()
{
	cin >> d >> n;

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

	for (int i = 0; i < n; ++i) scanf("%d %d %d", &fuku[i].a, &fuku[i].b, &fuku[i].c);

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

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

	int ans = 0;

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

	printf("%d\n", ans);
}

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

using namespace std;

int main(void){

  int D,N,t,a,b,c;
  vector<int>T,A,B,C;

  while(cin >> D >> N){
    T.clear();
    A.clear();
    B.clear();
    C.clear();
      
    for(int i=0;i<D;i++){
      cin >> t;
      T.push_back(t);
    }

    for(int i=0;i<N;i++){
      cin >> a >> b >> c;
      A.push_back(a);
      B.push_back(b);
      C.push_back(c);
    }

    int p,ans[200];
    for(int i=0;i<200;i++)ans[i]=0;

    for(int i=0;i<N;i++){
      if(A[i]<=T[0] && T[0]<=B[i]){
	p=C[i];

	for(int j=1;j<D;j++){
	  int mx=0;
	  for(int k=0;k<N;k++){
	    if(A[k]<=T[j] && T[j]<=B[k] && mx<abs(p-C[k])){
	      mx=abs(p-C[k]);
	      p=C[k];
	    }
	  }
	  ans[i]+=mx;
	}
      }
    }
    int answer=0;
    for(int i=0;i<200;i++)answer=max(answer,ans[i]);
    cout << answer << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
static const int INF = 8931919114514810;
static const int MAX_N = 300;
using namespace std;

int A[300],B[300],C[300];
int mem[MAX_N][MAX_N];
int N,D;
int Day[MAX_N];
int cal(int day,int pp){
   int res=0;
   if(day==N+1){
        return res=0;
   }
   if(mem[day][pp]!=-1){
        return mem[day][pp];
   }
    if(day<=1){
       FOR(i,1,D){
            if(Day[day]>=A[i]&&Day[day]<=B[i]){
            res=cal(day+1,i);
         }
      }
   }
   else{
        FOR(i,1,D){
            if(Day[day]>=A[i]&&Day[day]<=B[i]){
                int temp=abs(C[pp]-C[i]);
                res=max(cal(day+1,i)+temp,res);
                mem[day][pp]=res;
            }
        }
   }
   return mem[day][pp]=res;
}

int main(){
    cin>>N>>D;
    memset(mem,-1,sizeof(mem));
    FOR(i,1,N){
        cin>>Day[i];
    }
    FOR(i,1,D){
        cin>>A[i]>>B[i]>>C[i];
    }
    int ans=0;
    FOR(i,1,D){
        if(Day[1]>=A[i]&&Day[1]<=B[i]){
            ans=max(cal(1,i),ans);
        }
    }
    cout<<ans<<endl;
    return 0;
}