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 FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
static const LL INF = 1<<24;
static const int MAX_N = 300;
using namespace std;

struct clothes{
    LL A[MAX_N],B[MAX_N],C[MAX_N];
};

LL mem[MAX_N][MAX_N];
int N,D;
int Day[MAX_N];
int ans=0;
clothes T;
LL cal(int day,int pp){
   LL res=0;
   //cout<<res<<" "<<day<<endl;
   if(day==N+1){
        return 0;
   }
   else if(mem[day][pp]!=-1){
        //cout<<"hoge"<<endl;
        return mem[day][pp];
   }
   else{
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]&&pp!=T.C[i]){
                if(day<=1){
                    res=cal(day+1,T.C[i]);
                }
                else {
                    res=max(cal(day+1,T.C[i])+abs(pp-T.C[i]),res);
                    mem[day][pp]=res;
                }
            }
        }
   }
   return mem[day][pp]=res;
}

int main(){
    //cout<<-INF<<endl;
    cin>>N>>D;
    fill(mem[0],mem[MAX_N],-1);
    FOR(i,1,N){
        cin>>Day[i];
    }
    FOR(i,1,D){
        cin>>T.A[i]>>T.B[i]>>T.C[i];
    }
    cout<<cal(1,0)<<endl;
}

C++: /* {{{ Shinobu kawaii */
/*
  ______   __        _                  __                 
.' ____ \ [  |      (_)                [  |                
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _   
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |  
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |, 
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/ 

*/
/* }}} */

#include <bits/stdc++.h>
using namespace std;

// #define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* typedef */
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> pii;
typedef double D;
typedef complex<D> P;

/* const */
const int INF = 999999999;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; std::cin >> x; return x;}
template <typename T> void print(T x) {std::cout << x << '\n';}
template <typename T>
void print(std::vector<T>& v, string s = " ") {
  REP(i, v.size()) {
    if (i != 0) std::cout << s;
    std::cout << v[i];
  }
  std::cout << '\n';
}

/* }}} */

int d, n;
vi t(200);
vi a(200), b(200), c(200);
vii dp(210, vi(210, 0));

signed main()
{
  cin >> d >> n;
  REP(i, d) t[i] = in();
  REP(i, n) cin >> a[i] >> b[i] >> c[i];

  REP(i, d - 1) {
    REP(j, n) {
      if (a[j] <= t[i] && t[i] <= b[j]) {
        REP(k, n) {
          if (a[k] <= t[i + 1] && t[i + 1] <= b[k]) {
            dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(c[j] - c[k]));
          }
        }
      }
    }
  }
  int ans = -INF;
  REP(i, n) ans = max(ans, dp[d - 1][i]);
  print(ans);

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<math.h>
using namespace std;
typedef long long int ll;

int main()
{
	int d,n;
	int t[200];
	int a[200],b[200],c[200];
	int ans1=0,ans2=0;
	int ma[200],mi[200];

	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",&a[i],&b[i],&c[i]);
	for(int i=0;i<d;i++){
		ma[i]=-1;
		mi[i]=101;
	}

	for(int i=0;i<d;i++){
		for(int j=0;j<n;j++){
			if(a[j]<=t[i]&&t[i]<=b[j]){
				ma[i]=max(ma[i],c[j]);
				mi[i]=min(mi[i],c[j]);
			}
		}
	}

	for(int i=0;i<d-1;i++){
		if(i%2==0){
			ans1+=abs(ma[i]-mi[i+1]);
			ans2+=abs(mi[i]-ma[i+1]);
		}
		else{
			ans2+=abs(ma[i]-mi[i+1]);
			ans1+=abs(mi[i]-ma[i+1]);
		}
	}

	printf("%d\n",max(ans1,ans2));
	return 0;
}

C++: #include <iostream>
#include <cmath>
using namespace std;
int tem[201],G[201][3],D,N,DP[201][201]={0};
int absn(int value){
	if(value>0)
		return value;
	return -value;
}
int main() {
	cin >> D >> N ;
	for (int i=1;i<=D;i++){
		cin>>tem[i];
	}
	for(int i=0;i<N;i++){
		for(int j=0;j<3;j++){
			cin >> G[i][j];

		}
		if(!(G[i][0]<=tem[1] && tem[1]<=G[i][1]))
			DP[i][1]=-9999;
	}

	for(int i=2;i<=D;i++){
		for(int j=0;j<N;j++){
			if(G[j][0]<=tem[i] && tem[i]<=G[j][1]){
			for(int k=0;k<N;k++){
			DP[j][i]=max(DP[j][i],DP[k][i-1]+absn(G[k][2]-G[j][2]));
			}
			}
		}
	}
	int maxer=-1;
	for (int i=0;i<N;i++){
		maxer=max(maxer,DP[i][D]);
	}
	cout << maxer << endl; 
}

C++: //56
#include<iostream>
#include<cstdlib>
#include<algorithm>

using namespace std;

int main(){
  int D,N;
  cin>>D>>N;
  int T[200];
  for(int i=0;i<D;i++){
    cin>>T[i];
  }
  int A[200],B[200],C[200];
  for(int i=0;i<N;i++){
    cin>>A[i]>>B[i]>>C[i];
  }
  int dp[200][200];
  fill(dp[0],dp[200],1<<31);
  for(int i=0;i<N;i++){
    if(A[i]<=T[0]&&T[0]<=B[i]){
      dp[0][i]=0;
    }
  }
  for(int i=1;i<D;i++){
    for(int j=0;j<N;j++){
      if(A[j]<=T[i]&&T[i]<=B[j]){
	for(int k=0;k<N;k++){
	  dp[i][j]=max(dp[i][j],dp[i-1][k]+abs(C[j]-C[k]));
	}
      }
    }
  }
  cout<<*max_element(dp[D-1],dp[D])<<endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#define abs(x) (x>0?x:-(x))
#define MAX(x,y) (x>y?x:y)
#define D 200
#define N 200
#define INF 1000000
using namespace std;

typedef struct{
	int max_t,min_t,c;
} clothes;

int main(){
	clothes clt[N];
	int t[D];
	int d,n;
	cin>>d>>n;

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

	for(int i=0;i<n;i++){
		cin>>clt[i].min_t>>clt[i].max_t>>clt[i].c;
	}

	int dp[D][N];
	
	for(int i=0;i<d;i++){
		for(int j=0;j<n;j++){
			if(i==0 && clt[j].min_t<=t[0] && clt[j].max_t>=t[0]) dp[i][j]=0;
			else dp[i][j]=-INF;
		}
	}

	for(int i=1;i<d;i++){
		for(int j=0;j<n;j++){
			if(clt[j].min_t<=t[i] && clt[j].max_t>=t[i]){
				for(int k=0;k<n;k++){
					dp[i][j]=MAX(dp[i][j],dp[i-1][k]+abs(clt[k].c-clt[j].c));
				}
			}
		}
	}
	
	int m=0;
	for(int i=0;i<n;i++){
		m=MAX(m,dp[d-1][i]);
	}

	cout<<m<<endl;
	return 0;
}