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;

int main() {
    int D, N, T[210], A[210], B[210], C[210];
    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 dp[D + 1][N];
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < D; ++i) {
        if (A[i] <= T[0] && T[0] <= B[i]) dp[0][i] = 0;
    }

    for (int i = 0; i < D - 1; ++i) {
        for (int j = 0; j < N; ++j) {
            if (dp[i][j] != -1) {
                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] + abs(C[k] - C[j]));
                    }
                }
            }
        }
    }

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

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int d;
    int n;
    cin>>d>>n;
    int t[200];
    int a[200];
    int b[200];
    int c[200];
    int dp[200][200];
    for(int i=0;i<d;i++){
    cin>>t[i];
    }
    for(int i=0;i<n;i++){
    cin>>a[i]>>b[i]>>c[i];
    }
    for(int ii=0;ii<d;ii++){
    for(int i=0;i<n;i++){
        if(a[i]<=t[ii]&&b[i]>=t[ii]){
        dp[ii][i] = 0;
        }else{
            dp[ii][i] = -1;
        }
    }}
    for(int i=1;i<d;i++){
        for(int ii=0;ii<n;ii++){
        if(a[ii]<=t[i]&&b[ii]>=t[i]){
            for(int iii=0;iii<n;iii++){
            if(dp[i-1][iii]>=0){
                dp[i][ii] = max(dp[i][ii],dp[i-1][iii]+abs(c[ii]-c[iii]));
            }}
        }else{
            dp[i][ii] = -1;
        }
        }
        }
        int res=0;
        for(int i=0;i<n;i++){
            res = max(res,dp[d-1][i]);
        }
        cout<<res<<endl;
return 0;
}

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

#define times(n, i)      uptil(0, n, i)
#define upto(f, t, i)    for(decltype(t) _##i = (t), i = (f); i <= _##i; i++)
#define uptil(f, t, i)   for(decltype(t) _##i = (t), i = (f); i < _##i; i++)
#define downto(f, t, i)  for(decltype(t) _##i = (t), i = (f); i >= _##i; i--)
#define downtil(f, t, i) for(decltype(t) _##i = (t), i = (f); i > _##i; i--)
#define unless(c) if(!(c))
#define until(c) while(!(c))
#define loop while(true)

#define all(v) v.begin(), v.end()
#define ln << "\n"

struct line{};

template<class T> istream& operator>>(istream& in, vector<T>& v) {
    times(v.capacity(), i) in >> v[i];
    return in;
}

template<class T> const ostream& operator<<(ostream& out, const vector<T>& v) {
    times(v.size(), i) out << (i ? " " : "") << v[i];
    return out;
}

template<class T, class...U> typename enable_if<is_same<T, line>::value, string>::type next(istream& in, const U&... args) {
    string s;
    getline(in, s, args...);
    return move(s);
}

template<class T, class...U> typename enable_if<!is_same<T, line>::value, T>::type next(istream& in, const U&... args) {
    T t(args...);
    in >> t;
    return move(t);
}
template<class T, class...U> auto next(const U&... args) -> decltype(next<T>(cin)) {
    return next<T>(cin, args...);
}

template<class T> T sum(const vector<T>& v) {
    T ans = T();
    for(const T& t : v) ans += t;
    return ans;
}

template<class T> T sum(const typename vector<T>::iterator& begin, const typename vector<T>::iterator& end) {
    T ans = T();
    for(typename vector<T>::iterator t = begin; t != end; t++) ans += *t;
    return ans;
}

template<class T> T min(const vector<T>& v) {
    return *min_element(v.begin(), v.end());
}

template<class T> T min(const typename vector<T>::iterator& begin, const typename vector<T>::iterator& end) {
    return *min_element(begin, end);
}

template<class T> T max(const vector<T>& v) {
    return *max_element(v.begin(), v.end());
}

template<class T> T max(const typename vector<T>::iterator& begin, const typename vector<T>::iterator& end) {
    return *max_element(begin, end);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int D = next<int>(), N = next<int>();
    auto T = next<vector<int>>(D);
    vector<vector<int>> c(D);
    times(N, j) {
        int A = next<int>(), B = next<int>(), C = next<int>();
        times(D, i) {
            if(A <= T[i] && T[i] <= B) c[i].push_back(C);
        }
    }
    int ans_max, before_max, ans_min, before_min;
    times(D, i) {
        int a = min(c[i]), b = max(c[i]);
        if(i) {
            int ans_min_tmp = ans_min;
            ans_min = max(ans_min + abs(a - before_min), ans_max + abs(a - before_max));
            ans_max = max(ans_min_tmp + abs(b - before_min), ans_max + abs(b - before_max));
            before_min = a;
            before_max = b;
        } else {
            ans_max = ans_min = 0;
            before_min = a;
            before_max = b;
        }
        #ifdef debug
            cout << "[" << a << "," << b << "] " << ans_min << " " << ans_max ln;
        #endif
    }
    cout << max(ans_min, ans_max) ln;

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[200],b[200],c[200],d,n,t[200];
int p[200][200] = {0};

int rec(int day, int prev)
{
  if(day == d)return 0;
  if(p[day][prev] != -1)return p[day][prev];

  int ans = 0;

  for(int now=0; now<n; now++){
    if(a[now] <= t[day] && b[now] >= t[day]){
      ans = max(ans,rec(day+1,now) + abs(c[now] - c[prev]));
    }
  }
  return p[day][prev] = ans;
}

  
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];

  for(int i=0; i<200; i++){
    for(int j=0; j<200; j++){
      p[i][j] = -1;
    }
  }

  int fans = 0;

  for(int i=0; i<n; i++){
    if(a[i] <= t[0] && t[0] <= b[i]){
      fans = max(fans,rec(1,i));
    }
  }
  cout<<fans<<endl;

}

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

int D, N;
int T[202];
int A[202], B[202], C[202];
int dp[202][202]; //[テ、ツスツ陛ヲツ猟・テァツ崢ョ][テ・ツ可催ヲツ猟・テァツ敖?」ツ?淌ヲツ慊江

bool check(int day, int ai){
  if(A[ai] <= T[day] && T[day] <= B[ai]){
    return true;
  }
  return false;
}

void solve(){
  memset(dp, -1, sizeof(dp));

  for(int j = 0; j < N; j++){
    if(check(0, 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(!check(i + 1, k)) continue;
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(C[j] - C[k]));
      }
    }
  }

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

int main(){
  while(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];
    }
    solve();
  }
}