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<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int D, N, T[400], A[400], B[400], C[400], X[20][400] = { 0 },hade[10][400];
	int F[400] = { 0 };
	int Z[400] = { 0 };
	int f[400];
	int ans;
	scanf("%d %d", &D, &N);
	for (int i = 1; i <= D; i++) {
		scanf("%d", &T[i]);
	}
	for (int i = 1; i <= N; i++) {
		scanf("%d %d %d", &A[i], &B[i], &C[i]);
	}
	for (int i = 1; i <= D; i++) {
		X[1][i] = 100;
	}
	for (int i = 1; i <= D; i++) {
		for (int j = 1; j <= N; j++) {
			if (A[j] <= T[i] && B[j] >= T[i] && C[j] > X[0][i]) {
				X[0][i] = C[j];
				hade[0][i] = j;
			}
			if (A[j] <= T[i] && B[j] >= T[i] && C[j] < X[1][i]) {
				X[1][i] = C[j];
				hade[1][i] = j;
			}
		}
	}
	if (D % 2 == 0) {
		for (int i = 0; i <= 1; i++) {
			for (int j = 0; j <= 1; j++) {
				if (abs(X[i][1] - X[j][2]) > F[1]) {
					F[2] = abs(X[i][1] - X[j][2]);
					f[2] = j;
					Z[2] = X[j][2];
				}
			}
		}
		for (int k = 4; k <= D; k+=2) {
			for (int i = 0; i <= 1; i++) {
				for (int j = 0; j <= 1; j++) {
					if (F[k-2]+abs(Z[k-2] - X[i][k-1])+abs(X[i][k-1]-X[j][k]) > F[k]) {
						F[k] = F[k - 2] + abs(Z[k - 2] - X[i][k - 1]) + abs(X[i][k - 1] - X[j][k]);
						f[k] = j;
						Z[k] = X[j][k];
					}
				}
			}
		}
		ans = F[D];
	}
	if (D % 2 == 1) {
		Z[1] = X[0][1];
		f[1] = 0;
		for (int k = 3; k <= D; k += 2) {
			for (int i = 0; i <= 1; i++) {
				for (int j = 0; j <= 1; j++) {
					if (F[k - 2] + abs(Z[k - 2] - X[i][k - 1]) + abs(X[i][k - 1] - X[j][k]) > F[k]) {
						F[k] = F[k - 2] + abs(Z[k - 2] - X[i][k - 1]) + abs(X[i][k - 1] - X[j][k]);
						f[k] = j;
						Z[k] = X[j][k];
					}
				}
			}
		}
		ans = F[D];
		Z[1] = X[0][1];
		f[1] = 0;
		for (int k = 3; k <= D; k += 2) {
			for (int i = 0; i <= 1; i++) {
				for (int j = 0; j <= 1; j++) {
					if (F[k - 2] + abs(Z[k - 2] - X[i][k - 1]) + abs(X[i][k - 1] - X[j][k]) > F[k]) {
						F[k] = F[k - 2] + abs(Z[k - 2] - X[i][k - 1]) + abs(X[i][k - 1] - X[j][k]);
						f[k] = j;
						Z[k] = X[j][k];
					}
				}
			}
		}
		ans = F[D];
		F[D] = 0;
		Z[1] = X[1][1];
		f[1] = 1;
		for (int k = 3; k <= D; k += 2) {
			for (int i = 0; i <= 1; i++) {
				for (int j = 0; j <= 1; j++) {
					if (F[k - 2] + abs(Z[k - 2] - X[i][k - 1]) + abs(X[i][k - 1] - X[j][k]) > F[k]) {
						F[k] = F[k - 2] + abs(Z[k - 2] - X[i][k - 1]) + abs(X[i][k - 1] - X[j][k]);
						f[k] = j;
						Z[k] = X[j][k];
					}
				}
			}
		}
		ans = max(ans, F[D]);
	}
	printf("%d\n",ans);
	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {

	int min = all[0];
	for (int i = 1; i < n; ++i) {
		if (all[0] > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[0];
	for (int i = 1; i<n; ++i) {
		if (all[0]<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}

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

#define D 200
#define N 200

#define max(a,b) (a>b?a:b)

int main() {
	int d, n, t[D], a[N], b[N], c[N], dp[D][N], ans=0;
	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<n; ++i) dp[0][i] = (a[i]<=t[0]&&t[0]<=b[i]?0:-1);
	for(int i=1; i<d; ++i) {
		for(int j=0; j<n; ++j) {
			dp[i][j] = 0;
			if(a[j]<=t[i]&&t[i]<=b[j]) {
				for(int k=0; k<n; ++k) {
					if(dp[i-1][k]!=-1) {
						dp[i][j] = max(dp[i][j], dp[i-1][k] + abs(c[j]-c[k]));
					}
				}
			}else dp[i][j] = -1;
		}
	}
	for(int i=0; i<n; ++i) ans = max(ans, dp[d-1][i]);
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdlib>  // abs() for integer
#include <queue>

using namespace std;



int main(){
    
    int Dmaxmin[201][3]={};
    priority_queue<int>max1[201];
    priority_queue<int, vector<int>, greater<int> >min[201];
    int day__max_min[201][3]={};
    int copy={};
    int maxio={};
    int minio={};
    int minmaxH[201][4]={};
    int maxtem[201]={};
    int D,N;
    


    cin >> D >> N;
    
    for(int i=1;i<=D;i++){
        cin >> maxtem[i];
    }
    
    for(int i=1;i<=N;i++){
        for(int t=1;t<=3;t++){
            cin >> minmaxH[i][t];
        }
    }
    
    for(int i=1;i<=D;i++){//i?????\??????
        for(int p=1;p<=N;p++){//p????????????
            if(minmaxH[p][1]<=maxtem[i] && maxtem[i]<=minmaxH[p][2]){
                max1[i].push(minmaxH[p][3]);//???????????????????????\??????
                min[i].push(minmaxH[p][3]);//???????????????????????\??????
            }
        }
    }
    
    
    for(int i=1;i<=D;i++){
        day__max_min[i][1]=max1[i].top();
        day__max_min[i][2]=min[i].top();
    }
    
    for(int i=2;i<=D;i=i+2){
        copy=day__max_min[i][1];
        day__max_min[i][1]=day__max_min[i][2];
        day__max_min[i][2]=copy;
    }
    
    for(int i=1;i<=D-1;i++){
        maxio+=abs(day__max_min[i][1]-day__max_min[i+1][1]);
    }
    
    for(int i=1;i<=D-1;i++){
        minio+=abs(day__max_min[i][2]-day__max_min[i+1][2]);
    }
    
    
    cout << max(maxio,minio) << endl;
    

    return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;

struct CLOTHES{
    int a;
    int b;
    int c;
};

int dp[256][256] = { 0 };

int main(){
    int d,n;
    cin >> d >> n;
    int t[256];
    for(int i = 1; i <= d; i++){
        cin >> t[i];
    }
    CLOTHES clo[256];
    for(int i = 1; i <= n; i++){
        cin >> clo[i].a >> clo[i].b >> clo[i].c;
    }
    
    for(int i = 1; i <= d; i++){
        for(int i2 = 1; i2 <= n; i2++){
            if(t[i] >= clo[i2].a && t[i] <= clo[i2].b){
                if(i == 1){ dp[i][i2] = abs(0 - clo[i2].c); }
                else{
                    for(int j = 1; j <= n; j++){
                        if(dp[i - 1][j] != -1){
                            if(i != 2){
                                dp[i][i2] = max(dp[i][i2], dp[i - 1][j]+abs(clo[j].c - clo[i2].c));
                            }
                            else { dp[i][i2] = max(dp[i][i2], abs(clo[j].c - clo[i2].c)); }
                        }
                    }
                }
            }
            else{ dp[i][i2] = -1; }
        }
    }

    int ans = -1;
    for(int i = 1; i <= n; i++){ ans = max(ans, dp[d][i]); }
    cout << ans << endl;
}