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 <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

struct cloth{
	cloth(int a, int b, int c):maxtem(a), mintem(b), gaudy(c){}
	int maxtem;
	int mintem;
	int gaudy;
};

struct state{
	state(bool a, int b):mark(a), count(b){}
	bool mark;
	int count;
};

int main(){
	int d, n;
	cin >> d >> n;
	vector<int> maxtem(d, 0);
	for(int i = 0; i < d; i++){
		cin >> maxtem[i];
	}
	vector<cloth> c(n, cloth(0, 100, 0));
	for(int i = 0; i < n; i++){
		cin >> c[i].mintem >> c[i].maxtem >> c[i].gaudy;
	}

	vector<vector<state> > table(d, vector<state>(101, state(false, 0)));
	for(int i = 0; i < d; i++){
		for(int j = 0; j < n; j++){
			if(maxtem[i]>=c[j].mintem&&maxtem[i]<=c[j].maxtem){
				if(!i){
					table[i][c[j].gaudy].mark = true;
					continue;
				}
				int max = 0;
				for(int k = 0; k < table[i-1].size(); k++){
					if(table[i-1][k].mark){
						int tmp = c[j].gaudy-k;
						if(tmp<0) tmp *= -1;
						tmp += table[i-1][k].count;
						if(max<=tmp){
							max = tmp;
						}
					}
				} 
				table[i][c[j].gaudy].mark = true;
				table[i][c[j].gaudy].count = max;
			}
		}
	}
	
	/*for(int i = 0; i < d; i++){
		for(int j = 0; j < table[i].size(); j++){
			if(j) cout << " ";
			cout << table[i][j].count;
		}
		cout << endl;
	}*/

	int max = 0;
	for(int i = 0; i < table[d-1].size(); i++){
		if(max<table[d-1][i].count) max = table[d-1][i].count; 
	}
	cout << max << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main(){
	int d,n;
	cin>>d>>n;
	vi t(d),a(n),b(n),c(n);
	rep(i,0,d) cin>>t[i];
	rep(i,0,n) cin>>a[i]>>b[i]>>c[i];


	vi cMAX(d),cMIN(d);
	rep(i,0,d){
		int MAX=0,MIN=1000;
		rep(j,0,n){
			if(a[j]<= t[i] && t[i]<=b[j]){
				MAX=max(MAX,c[j]);
				MIN=min(MIN,c[j]);
			}
			cMAX[i]=MAX;
			cMIN[i]=MIN;
		}
	}

	int ans=0,dpM[300]={},dpm[300]={};
	rep(i,1,d){
		dpM[i]=max(dpM[i-1]+abs(cMAX[i]-cMAX[i-1]),dpm[i-1]+abs(cMAX[i]-cMIN[i-1]));
		dpm[i]=max(dpM[i-1]+abs(cMIN[i]-cMAX[i-1]),dpm[i-1]+abs(cMIN[i]-cMIN[i-1]));
		//cout<<dpM[i]<<" "<<dpm[i]<<endl;
	}
	ans=max(dpM[d-1],dpm[d-1]);
	o(ans);
}

C++: #include <stdio.h>
#include <algorithm>

int D, N, T[212], A[212], B[212], C[212];
int dp[212][212], memo[212], ans;

int main(void) {
	scanf("%d%d", &D, &N);
	for (int i = 0; i < D; ++i) { scanf("%d", &T[i]); }
	for (int i = 0; i < N; ++i) { scanf("%d%d%d", &A[i], &B[i], &C[i]); }

	for (int i = 0; i < D; ++i) {
		for (int j = 0; j < N; ++j) {
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < D; ++i) {
		for (int j = 0; j < N; ++j) {
			if (A[j] <= T[i] && T[i] <= B[j]) {
				if (i == 0) { dp[i][j] = C[j]; }
				else {
					for (int k = 0; k < N; ++k) {
						if (dp[i - 1][k] != -1) {
							dp[i][j] = dp[i - 1][k] + abs(C[k] - C[j]);
							memo[i] = std::max(memo[i], dp[i][j] - dp[i - 1][k]);
						}
					}
				}
			}
		}
	}

	for (int i = 0; i < D; ++i) {
		ans += memo[i];
	}
	printf("%d\n", ans);
	return 0;
}

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

#define INT32_MAX 2147483647

using namespace std;

struct cloth {
    int min,max,c;

    bool operator<(const cloth& right) const {
        return c < right.c;
    }
};

int d;
int t[201];
cloth clothes[201];
int wearablec[201][201];
int wearablenum[201];
//[day][prec]
int mem[201][101];

inline bool isrange(int temp, cloth *c) {
    return (c->min <= temp) && (temp <= c->max);
}

int cal(int today, int prec) {
    if (today == d) {
        return 0;
    }

    if (mem[today][prec] != -1) {
        return mem[today][prec];
    }

    int todaymax = -1;
    for (int i = 0; i < wearablenum[today]; ++i) {
        int temp = abs(prec - wearablec[today][i]) + cal(today + 1, wearablec[today][i]);
        if (todaymax < temp) {
            todaymax = temp;
        }
    }

    mem[today][prec] = todaymax;
    return todaymax;
}

int main() {
    for (int i = 0; i < 201; ++i) {
        for (int j = 0; j < 101; ++j) {
            mem[i][j] = -1;
        }
    }
    int n;
    cin >> d >> n;
    for (int i = 0; i < d; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> clothes[i].min >> clothes[i].max >> clothes[i].c;
    }

    for (int i = 0; i < d; ++i) {
        int num = 0;
        for (int j = 0; j < n; ++j) {
            if (isrange(t[i], &clothes[j])) {
                wearablec[i][num] = clothes[j].c;
                num++;
            }
        }
        wearablenum[i] = num;
    }

    int max = -1;
    for (int i = 0; i < wearablenum[0]; ++i) {
        int temp = cal(1, wearablec[0][i]);
        if (max < temp) {
            max = temp;
        }
    }

    cout << max << endl;

    return 0;
}

C++: #include <iostream>

using namespace std;

#define REP(i, s, e) for(int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;


ll dp[210][210];
int t[210];
int a[210], b[210], c[210];

int main(){
	int d, n;
	cin>>d>>n;
	rep(i, d) cin>>t[i];
	rep(i, n) cin>>a[i]>>b[i]>>c[i];

	rep(i, 210) rep(j, 210) dp[i][j] = -1e18;
	rep(i, n) if(a[i] <= t[0] && t[0] <= b[i]) dp[0][i] = 0LL;

	REP(i, 0, d-1){
		rep(j, n) {
			if(a[j] <= t[i+1] && t[i+1] <= b[j]){
				rep(k, n) {
					dp[i+1][j] = max(dp[i+1][j], dp[i][k] + abs(c[j] - c[k]));
				}
			}
		}
	}
	
	ll ans = 0LL;
	rep(i, n) {
		ans = max(ans, dp[d-1][i]);
	}

	cout<<ans<<endl;
}