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<stdio.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;

struct str
{
	int a, b, c;
	void new_str(int a_, int b_, int c_){
		a = a_; b = b_; c = c_;
	}
};
int dp[200][200];
int t[200];
str cl[200];
int main(){
	int d, n;
	scanf("%d%d", &d, &n);
	memset(dp, -1, sizeof(dp));
	for (int i = 0; i < d; i++)
		scanf("%d", &t[i]);
	for (int i = 0; i < n; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		cl[i].new_str(a, b, c);
	}
	for (int i = 0; i < n; i++)
		if (cl[i].a <= t[0] && t[0] <= cl[i].b)
			dp[0][i] = 0;
	for (int i = 1; i < d; i++){
		for (int j = 0; j < n; j++){
			if (cl[j].a <= t[i] && t[i] <= cl[j].b){
				for (int l = 0; l < n; l++){
					if (dp[i - 1][l] >= 0 && abs(cl[l].c - cl[j].c) + dp[i - 1][l]>dp[i][j])
						dp[i][j] = abs(cl[l].c - cl[j].c) + dp[i - 1][l];
				}
			}
		}
	}
	printf("%d\n", *max_element(dp[d - 1], dp[d - 1] + n));
	return 0;
}

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

using namespace std;

int main(void){

  int d,n,t,a,b,c,dp[202][202];
  vector<int>T,A,B,C;

  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);
    }

    for(int i=0;i<202;i++)
      for(int j=0;j<202;j++)
	dp[i][j]=-1;

    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
       
	  if(A[i]<=T[0] && T[0]<=B[i] && A[j]<=T[1] && T[1]<=B[j])
	    dp[0][j]=max(dp[0][j],abs(C[i]-C[j]));
	}
    }

    for(int i=1;i<=d;i++){
      for(int j=0;j<n;j++){
	for(int k=0;k<n;k++){
	    if(A[k]<=T[i] && T[i]<=B[k] && dp[i-1][j]>=0)
	      dp[i][k]=max(dp[i][k],dp[i-1][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 << endl;

  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};
 
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define INF 1000000000
ll d,n,t[300],a[300],b[300],c[300];
ll dp[300][200];
int main(){
   cin >> d >> n;

  REP(i,d)
      cin >> t[i];
  REP(i,n)
      cin >> a[i] >> b[i] >> c[i];

 REP(i,101)
      dp[0][i] = -INF;

  REP(i,n)
      if(a[i] <= t[0] && t[0] <= b[i])
          dp[0][c[i]] = 0;

  REP(i,d-1){
      REP(j,101)
          dp[i + 1][j] = -INF;
      REP(j,101){
          if(dp[i][j] < 0)
              continue;

          REP(k,n){
              if(a[k] <= t[i + 1] && t[i + 1] <= b[k]){
                  dp[i + 1][c[k]] = max(dp[i + 1][c[k]],dp[i][j] + abs(j - c[k]));
              }
          }
      }
  }
  ll ans = 0;
  REP(i,101)
      ans = max(ans,dp[d - 1][i]);
  printf("%lld\n",ans);
  return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int d,n;
	int t[201];
	int a[201],b[201],c[201];
	int c1;
	int i,j,k;
	int kei=0,max=-1;
	int dp[201][201];
	
	scanf("%d %d",&d,&n);
	for(i=0;i<d;i++){
		for(j=0;j<n;j++){
			dp[i][j]=0;
		}
	}
	for(i=0;i<d;i++){
		scanf("%d",&t[i]);
	}
	for(i=0;i<n;i++){
		scanf("%d %d %d",&a[i],&b[i],&c[i]);
	}
	for(i=0;i<d-1;i++){
		for(j=0;j<n;j++){
			if(t[i]>=a[j] && t[i]<=b[j]){
				for(k=0;k<n;k++){
					if(t[i+1]>=a[k] && t[i+1]<=b[k]){
						c1=c[j]-c[k];
						if(c1<0){
							c1=c1*-1;
						}
						if(c1+dp[i][j]>dp[i+1][k]){
							dp[i+1][k]=dp[i][j]+c1;
						}
					}
				}
			}
		}
	}
	for(i=0;i<n;i++){
		if(dp[d-1][i]>max){
			max=dp[d-1][i];
		}
	}
	printf("%d\n",max);
	return 0;
}

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

int dat[210],kmi[210],kma[210],had[210],dp[210][210];//dp[??\][???]
int main(){
	int D,N,cnt;
	cin>>D>>N;
	for(int i=0;i<D;i++){
		cin>>dat[i];
	}
	for(int i=0;i<N;i++){
		cin>>kmi[i]>>kma[i]>>had[i];
	}
	for(int i=0;i<=D;i++)
		for(int j=0;j<N;j++)
			dp[i][j]=-1;
	for(int i=0;i<N;i++){
		if(dat[0]>=kmi[i]&&dat[0]<=kma[i]){
			dp[0][i]=0;
		}
	}

	for(int i=0;i<D-1;i++){
		for(int j=0;j<N;j++){
			if(dat[i+1]>=kmi[j]&&dat[i+1]<=kma[j]){
				cnt=-1;
				for(int k=0;k<N;k++){
					if(dp[i][k]!=-1)cnt=max(cnt,abs(had[k]-had[j])+dp[i][k]);
				}
				dp[i+1][j]=cnt;
			}
		}
	}

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

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

signed 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);   
    vector<int> b(n);   
    vector<int> c(n);
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i];
    
    vector<vector<int> > dp(d + 1, vector<int> (n, -1));
    for(int j = 0; j < n; j++){
        if(a[j] <= t[0] && t[0] <= b[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(a[k] <= t[i + 1] && t[i + 1] <= b[k]){
                    dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + (int)abs(c[j] - c[k]));
                }
            }
        }
    }

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

    return 0;
}