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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll; 
typedef pair<ll, ll> PL;

ll n, d, p = 0, dp1, dp2;
ll t[1000], a[1000], b[1000], c[1000], MAX[1000], MIN[1000];

int main(){
	cin >> n >> d;
	REP(i, n){
		cin >> t[i];
	}
	REP(i, d){
		cin >> a[i] >> b[i] >> c[i];
	}
	REP(i, n){
		ll ma = -1, mi = 10000;
		REP(j, d){
			if (t[i] >= a[j] && t[i] <= b[j]){
				ma = max(c[j], ma);
				mi = min(c[j], mi);
			}
		}
		MAX[i] = ma;
		MIN[i] = mi;
	}
	dp1 = MAX[0];
	dp2 = MIN[0];
	REP(i, n - 1){
		if (abs(MAX[i + 1] - dp2) >= abs(MIN[i + 1] - dp1)){
			p += abs(MAX[i + 1] - dp2);
			if (abs(MAX[i + 1] - dp2) == abs(MIN[i + 1] - dp1)){
				dp1 = MAX[i + 1];
				dp2 = MIN[i + 1];
			}
			else{
				dp1 = dp2 = MAX[i + 1];
			}
		}
		else{
			p += abs(MIN[i + 1] - dp1);
			dp1 = dp2 = MIN[i + 1];
		}
	}
	cout << p << endl;

}

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

using namespace std;

int main() {

	int d, n;
	cin >> d >> n;

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

	vector< vector<int> > dd(d, vector<int>());
	int a, b, c;
	for (int i = 0; i < n; i++) {
		cin >> a >> b >> c;
		for (int j = 0; j < d; j++) {
			if (a <= day[j] && day[j] <= b) {
				dd[j].push_back(c);
			}
		}
	}
	int ddd[220][2] = {0};
	int dp[220][2] = {0};
	for (int j = 0; j < d; j++) {
		sort(dd[j].begin(), dd[j].end());
		ddd[j][0] = dd[j][0];
		int sz = dd[j].size();
		ddd[j][1] = dd[j][sz-1];
		//cout << ddd[j][0] << ' ' << ddd[j][1] << endl;
	}

	for (int j = 1; j < d; j++) {
		dp[j][0] = max(abs(ddd[j][0]-ddd[j-1][0])+dp[j-1][0], abs(ddd[j][0]-ddd[j-1][1])+dp[j-1][1]);
		dp[j][1] = max(abs(ddd[j][1]-ddd[j-1][0])+dp[j-1][0], abs(ddd[j][1]-ddd[j-1][1])+dp[j-1][1]);
	}

	cout << max(dp[d-1][0], dp[d-1][1]) << endl;

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<cmath>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> Pi;
int D,N;

int dijkstra(vector<vector<int> >& list,vector<vector<int> >& used)
{

  /*
  for(int i=0;i<D;i++)
    {
      cout << i+1 << " day" << endl;
      for(int j=0;j<list[i].size();j++)
	{
	  cout << list[i][j] << " ";
	}
      cout << endl;
    }
  */
  
  int mex = 0;
  queue<Pi> que;// Pi(P(value,day),pre_index)
  for(int i=0;i<list[0].size();i++)
    {
      que.push(Pi(P(0,1),i));
      used[0][i] = 0;
    }
  
  while(!que.empty())
    {
      Pi p = que.front(); que.pop();
      int total,day,pre_index;
      total = p.F.F, day = p.F.S, pre_index = p.S;

      if(day >= D)
	{
	  mex = max(mex,total);
	  continue;
	}

      for(int i=0;i<list[day].size();i++)
	{
	  int sum = total + abs(list[day-1][pre_index]-list[day][i]);
	  if(used[day][i] >= sum)
	    continue;

	  used[day][i] = sum;
	  que.push(Pi(P(sum,day+1),i));
	}

    }

  return mex;
}


int main()
{

  cin >> D >> N;
  vector<int> T;
  vector<vector<int> > list,used;
  list.resize(D),used.resize(D);


  for(int i=0;i<D;i++)
    {
      int in; cin >> in;
      T.push_back(in);
    }

  for(int j=0;j<N;j++)
    {
      int A,B,C;
      cin >> A >> B >> C;
      for(int k=0;k<D;k++)
	{
	  if(A <= T[k] && T[k] <= B)
	    {
	      list[k].push_back(C),used[k].push_back(-1);
	    }
	}
    }
  cout << dijkstra(list,used) << endl;


  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;

ll n, d, p = 0, dp;
ll t[1000], a[1000], b[1000], c[1000], MAX[1000], MIN[1000];

int main(){
	cin >> n >> d;
	REP(i, n){
		cin >> t[i];
	}
	REP(i, d){
		cin >> a[i] >> b[i] >> c[i];
	}
	REP(i, n){
		ll ma = -1, mi = 10000;
		REP(j, d){
			if (t[i] >= a[j] && t[i] <= b[j]){
				ma = max(c[j], ma);
				mi = min(c[j], mi);
			}
		}
		MAX[i] = ma;
		MIN[i] = mi;
	}
	REP(i, n - 1){
		if (i == 0){
			if (abs(MAX[i + 1] - MIN[i]) > abs(MIN[i + 1] - MAX[i])){
				p += abs(MAX[i + 1] - MIN[i]);
				dp = MAX[i + 1];
			}
			else{
				p += abs(MIN[i + 1] - MAX[i]);
				dp = MIN[i + 1];
			}
		}
		else{
			if (abs(MAX[i + 1] - dp) > abs(MIN[i + 1] - dp)){
				p += abs(MAX[i + 1] - dp);
				dp = MAX[i + 1];
			}
			else{
				p += abs(MIN[i + 1] - dp);
				dp = MIN[i + 1];
			}
		}
	}
	cout << p << endl;
}