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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



int D, N;
int T[1000];
vint ok[1000];
int C[1000];

int dp[1000][1000];

int main() {
    while (cin >> D >> N) {
        for (int i = 0; i < D; ++i) {
            cin >> T[i];
            ok[i].clear();
        }
        for (int i = 0; i < N; ++i) {
            int a, b; cin >> a >> b;
            for (int j = 0; j < D; ++j) if (a <= T[j] && T[j] <= b) ok[j].PB(i);
            cin >> C[i];
        }
        
        memset(dp, -1, sizeof(dp));
        for (int i = 0; i < ok[0].size(); ++i) dp[1][ok[0][i]] = 0;
        
        int res = -1;
        for (int i = 1; i < D; ++i) {
            for (int j = 0; j < N; ++j) {
                if (dp[i][j] == -1) continue;
                
                //cout << i << ", " << j << " : " << dp[i][j] << endl;
                
                for (int k = 0; k < ok[i].size(); ++k) {
                    int nj = ok[i][k];
                    chmax(dp[i+1][nj], dp[i][j] + abs(C[nj] - C[j]));
                    
                    if (i == D-1) chmax(res, dp[i+1][nj]);
                }
            }
        }
        
        cout << res << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define REP(i, b, e) for ( int i = b; i < (int)e; i++ )
#define rep(i, n) REP(i, 0, n)
static const int DMAX = 200;
static const int NMAX = 200;
static const int INPOS = -1;

main(){
  int D, N, T[DMAX+1], C[NMAX+1], dp[NMAX+1][DMAX+1];
  pair<int, int> AB[NMAX+1];
  cin >> D >> N;
  rep(i, D) cin >> T[i+1]; // +1 for 1 base
  rep(i, N) cin >> AB[i+1].first >> AB[i+1].second >> C[i+1];
  rep(i, N+1) rep(j, D+1) dp[i][j] = INPOS;
  
  REP(i, 1, N+1)
    if ( AB[i].first <= T[1] && T[1] <= AB[i].second ) dp[i][1] = 0;

  REP(j, 2, D+1){
    REP(i, 1, N+1){
      if ( !( AB[i].first <= T[j] && T[j] <= AB[i].second) ) continue;
      REP(k, 1, N+1){
	if ( dp[k][j-1] == INPOS ) continue;
	dp[i][j] = max(dp[i][j], dp[k][j-1] + abs(C[k] - C[i]));
      }
    }
  }

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

C++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int 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), b(n), c(n);
    for(int i=0; i<n; ++i)
        cin >> a[i] >> b[i] >> c[i];

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

    cout << *max_element(dp[d-1].begin(), dp[d-1].end()) << endl;

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int D,N;
int temp[201];
int cloth[201][3];
int a[201][201][201];


int dp(int d,int yk,int k){
	if(d > 0){
		if(!(cloth[k][0] <= temp[d] && cloth[k][1] >= temp[d]))return 0;
	}

	if(a[d][yk][k] >= 0) return a[d][yk][k];

	if(d == D){
		return a[d][yk][k] = fabs(cloth[yk][2] - cloth[k][2]) ;
	}

	int eva = 0;
	int sum = 0;
	for(int i = 1; i <= N ;i++){
		if(d < 2){
			sum = dp(d+1, k, i);
			eva = max(eva,sum);
		}else{
			sum = dp(d +1, k, i) + fabs(cloth[yk][2] - cloth[k][2]);
			// cout <<d <<" "<< yk << " "<<k <<" "<< sum << endl;
			eva = max(eva,sum);
		}
	}

	return a[d][yk][k] = eva;


	// long long int eva = 0;
	// long long int sum = 0;
	// if(d >= 2){

	// 	eva = fabs(yeva - cloth[k][2]);
	// }

	// if(d == D){
	// 	// cout<<d<<" "<<k<<" "<<eva<<endl;
	// 	return a[d][k] = eva;
	// }


	// for(int i = 1;i <= N;i++){
	// 	if(d == 0){
	// 		eva = max(eva, dp(d+1, i, 0));
	// 	}else if( d == 1){
	// 		// cout<<sum<< " "<<eva<<endl;
	// 		// cout<<eva<<" "<<dp(d+1,i,cloth[k][2])<<endl;
	// 		eva = max(eva,dp(d+1, i, cloth[k][2]));
	// 	}else{
	// 		sum = dp(d+1,i,cloth[k][2]) + eva;
	// 		cout<<d<<" "<<dp(d+1,i,cloth[k][2])<<" "<<eva <<" "<<sum<<endl;
	// 		// cout<<eva<<" "<<dp(d+1,i,cloth[k][2])<<endl;
	// 		eva = max(eva,sum);
	// 	}
	// }
	// // cout << d <<" " <<k<<" " << eva <<endl; 
	// return a[d][k] = eva;
}


int main(){
	fill_n(temp,201,-1);
	fill_n(*cloth,201*3,-1);
	fill_n(**a,201*201*201,-1);
	cin >> D >> N;
	for(int i = 1;i <= D;i++){
		cin >>temp[i];
	}
	for(int i = 1;i <= N;i++){
		for(int j = 0;j < 3;j++){
			cin >> cloth[i][j];
		}
	}
	cout << dp(0,0,0) << endl;
}