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 := 1; 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 <iostream>
#include <stdio.h>
#include <string.h>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
#include <time.h>
#include <stdlib.h>

using namespace std;

int main (void)
{
	long Z[100][100],ZZ,n,m,a[100],b[100],c[100],d[100],huk[100][100],P;
	P = 0;

	cin >> n >> m;

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int j = 0; j < m; j++)
	{
		cin >> b[j] >> c[j] >> d[j];
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (b[j] <= a[i] && a[i] <= c[j])
			{
				huk[i][j] = d[j];
			}
			else
			{
				huk[i][j] = 0;
			}
		}
	}

	ZZ = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			P = 0;
			if (j != m)
			{
				P = huk[i][j] - huk[i][j + 1];
			}else
			{
				P = huk[i][j];
			}
			if (P < 0)
			{
				P = P - P - P;
			}

			Z[i][j] += P;
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (Z[i][j] < ZZ)
			{
				ZZ = 0;
				ZZ = Z[i][j];
			}
		}
	}


	cout << ZZ << endl;

}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}

int N,D;
int T[201];
int A[201];
int B[201];
int C[201];

int dp[201][201];

int dfs(int pos,int prv){
  if(dp[pos][prv]>=0)return dp[pos][prv];
  if(pos==D)return 0;
  int res=0;
  if(pos==0){
    for(int i=0;i<N;i++){
      if(A[i]<=T[pos]&&T[pos]<=B[i]){
        res=max(res,dfs(pos+1,C[i]));
      }
    }
  }
  else{
    for(int i=0;i<N;i++){
      if(A[i]<=T[pos]&&T[pos]<=B[i]){
        res=max(res,dfs(pos+1,C[i])+abs(prv-C[i]));
      }
    }
  }
  return dp[pos][prv]=res;
}

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];
  memset(dp,-1,sizeof(dp));
  cout<<dfs(0,0)<<endl;

  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	int D, N;
	int T[200];
	int dp[200][101];
	vector<int> C[61];
	cin >> D >> N;
	REP(i, D){
		cin >> T[i];
	}
	REP(i, N){
		int a, b, c;
		cin >> a >> b >> c;
		FOR(j, a, b+1){
			C[j].push_back(c);
		}
	}
	REP(i, 200){
		fill(dp[i], dp[i]+101, -1);
	}
	REP(i, C[T[0]].size()){
		dp[0][C[T[0]][i]] = 0;
	}
	FOR(i, 1, D){
		REP(j, C[T[i]].size()){
			int diff = 0;
			REP(k, 101){
				if(dp[i-1][k] == -1) continue;
				diff = max(diff, dp[i-1][k] + abs(C[T[i]][j] - k));
			}
			dp[i][C[T[i]][j]] = diff;
		}
	}
	ll res = 0;
	REP(i, 101){
		if(dp[D-1][i] != -1) res = max(res, (ll)dp[D-1][i]);
	}
	cout << res << endl;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (-1 * (1 << 29))
using namespace std;

struct Clothes{
    int low, up, glitz;
};

int dp[205][205], t[205];
Clothes c[205];

void update(int day, int now, int before){
    if(c[now].low <= t[day] && c[now].up >= t[day]){
        if(day == 0) dp[day][now] = 0; //テ・ツ按敕ヲツ猟・テ」ツ?ォテァツ敖?」ツつ凝ヲツ慊?
        else if(dp[day - 1][before] != INF){
            dp[day][now] = max(dp[day][now], dp[day - 1][before] + abs(c[now].glitz - c[before].glitz)); 
        }
    }
}

int solve(int d, int n){
    rep(day,d){
        rep(now,n){
            rep(before,n){
                update(day, now, before);
            }
        }
    }

/*
    rep(i,d){
        rep(j,n){
            cout << dp[i][j] << ' ';
        }
        cout << endl;
    }
*/

    int retval = 0;
    rep(i,n) retval = max(retval, dp[d - 1][i]);
    return retval;
}

int main(){
    int d, n;

    cin >> d >> n;
    rep(i,d) cin >> t[i];
    rep(i,n) cin >> c[i].low >> c[i].up >> c[i].glitz;
    rep(i,105) rep(j,105) dp[i][j] = INF;
    cout << solve(d, n) << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n,d;
  cin>>d>>n;
  int t[d];
  for(int i=0;i<d;i++) cin>>t[i];
  int a[n],b[n],c[n];
  for(int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i];
  int dp[d+1][101];
  memset(dp,-1,sizeof(dp));
  for(int i=0;i<n;i++){
    if(a[i]<=t[0]&&t[0]<=b[i])
      dp[1][c[i]]=0;
  }
  for(int i=1;i<d;i++){
    for(int j=0;j<101;j++){
      if(dp[i][j]<0) continue;
      for(int k=0;k<n;k++){
	if(a[k]<=t[i]&&t[i]<=b[k]){
	  dp[i+1][c[k]]=max(dp[i+1][c[k]],dp[i][j]+abs(j-c[k]));
	}
      }
    }
  }
  int ans=0;
  for(int i=0;i<101;i++)
    if(~dp[d][i]) ans=max(ans,dp[d][i]);
  cout<<ans<<endl;
  return 0;
}