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<stdlib.h>
using namespace std;
int main(){
	int d,n,i,i2,i3,dp[200][200],t[200],a[200],b[200],c[200],tk[200][200],max=0;
	cin>>d>>n;
	for(i=0;i<200;i++){
		for(i2=0;i2<200;i2++){
			dp[i][i2]=0;
			tk[i][i2]=0;
		}
	}
	for(i=0;i<d;i++){
		cin>>t[i];
	}
	for(i=0;i<n;i++){
		cin>>a[i]>>b[i]>>c[i];
	}
	for(i=0;i<d;i++){
		for(i2=0;i2<n;i2++){
			if(a[i2]<=t[i]&&t[i]<=b[i2]){
				tk[i][i2]=1;
			}
		}
	}
	for(i=0;i<d-1;i++){
		for(i2=0;i2<n;i2++){
			if(tk[i][i2]==1){
				for(i3=0;i3<n;i3++){
					if(tk[i+1][i3]==1){
						if(dp[i+1][i3]<dp[i][i2]+abs(c[i2]-c[i3])){
							dp[i+1][i3]=dp[i][i2]+abs(c[i2]-c[i3]);
						}
					}
				}
			}
		}
	}
	for(i=0;i<n;i++){
		if(max<dp[d-1][i]){
			max=dp[d-1][i];
		}
	}
	/*
	for(i=0;i<d;i++){
		for(i2=0;i2<n;i2++){
			cout<<dp[i][i2]<<' ';
			if(dp[i][i2]<10){
				cout<<' ';
			}
		}
		cout<<endl;
	}*/
	cout<<max<<endl;
}

C++: #include<stdio.h>
int i,j,a,b,c[200],d[200],e[200],f[200],g[200],h[1000][1000],m=0,k,kf=0,fkk=0,im=-1;
int main(void)
{
	scanf("%d %d",&a,&b);
	for(i=0;i<a;i++){
		scanf("%d",&c[i]);
	}
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			h[i][j]=-1;
		}
	}
	for(i=0;i<b;i++){
		scanf("%d %d %d",&d[i],&e[i],&f[i]);
	}
	for(i=0;i<b;i++){
		if(c[0]>=d[i] && c[0]<=e[i]){
			h[0][i]=0;
		}
	}
	for(i=0;i<a-1;i++){
		for(j=0;j<b;j++){
			if(h[i][j]!=-1){
				for(k=0;k<b;k++){
					if(c[i+1]>=d[k] && c[i+1]<=e[k]){
						kf=f[j]-f[k];
						if(kf<0){
							kf*=-1;
						}
						if(kf+h[i][j]>h[i+1][k]){
							h[i+1][k]=h[i][j]+kf;
						}
					}
				}
			}
		}
	}
	for(i=0;i<a;i++){
		if(m<h[a-1][i]){
			m=h[a-1][i];
		}
	}
	printf("%d\n",m);
	return 0;
}

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

using namespace std;

typedef struct {
	int a;
	int b;
	int c;
} fuku_t;

int d, n;

int t[200];
fuku_t fuku[200];

int dp[200][200];

int main()
{
	cin >> d >> n;

	for (int i = 0; i < d; ++i) cin >> t[i];

	for (int i = 0; i < n; ++i) scanf("%d %d %d", &fuku[i].a, &fuku[i].b, &fuku[i].c);

	for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
		if (fuku[j].a <= t[0] && t[0] <= fuku[j].b && fuku[i].a <= t[1] && t[1] <= fuku[i].b) {
			dp[1][i] = max(dp[1][i], abs(fuku[j].c - fuku[i].c));
		}
	}

	for (int i = 2; i < d; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) {
		if (fuku[j].a <= t[i] && t[i] <= fuku[j].b && fuku[k].a <= t[i - 1] && t[i - 1] <= fuku[k].b) {
			dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(fuku[k].c - fuku[j].c));
		}
	}

	int ans = 0;

	for (int i = 0; i < n; ++i) ans = max(ans, dp[d - 1][i]);

	printf("%d\n", ans);
}

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 = 0;
	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;
			}
		}
		//printf("%d %d %d\n", i, X[0][i], X[1][i]);
	}
	for (int a = 0; a <= 1; a++) {
		for (int b = 0; b <= 1; b++) {
			f[1] = a;
			f[2] = b;
			Z[1] = X[a][1];
			Z[2] = X[b][2];
			F[2] = abs(X[a][1] - X[b][2]);
			for (int k = 3; k <= D; k++) {
			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;
						f[k - 1] = i;
						Z[k] = X[j][k];
	
					}
				}
			}
		}
		//printf("%d %d %d\n", a,b,F[D]);
		ans = max(ans, F[D]);
		F[D] = 0;
		}
	}
	//for (int i = 1; i <= D; i++) {
		//printf("f(%d)=%d\n", i, f[i]);
	//}
	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 <algorithm>

using namespace std;

const int N = 200;
const int INF = (1<<28);

main(){
  int d, n, t[N], a[N], b[N], c[N], dp[200][100], 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];
  fill(dp[0], dp[2], -1);
  for(int i=0;i<n;i++) if(a[i] <= t[0] && t[0] <= b[i]) dp[0][c[i]] = 0;
  for(int i=1;i<d;i++){
    for(int j=0;j<=100;j++){
      if(dp[i-1][j] == -1) continue;
      for(int k=0;k<n;k++){
        if(a[k] <= t[i] && t[i] <= b[k]){
          int tmp = dp[i-1][j] + abs(c[k] - j);
          if(dp[i][c[k]] == -1 || dp[i][c[k]] < tmp) dp[i][c[k]] = tmp;
        }
      }
    }
  }
  for(int i=0;i<=100;i++) ans = max(ans, dp[d-1][i]);
  cout << ans << endl;
}

C++: #include<stdio.h>
int as[1001][1001];
int main(void)
{
	int x[1001],s[1001],s1[1001],s2[1001];
	int a,b,A,M=0,G=0,K=0,i,j,k;
	scanf("%d%d",&a,&b);
	for(i=1;i<=a;i++){
		scanf("%d",&x[i]);
	}
	for(i=0;i<b;i++){
		scanf("%d%d%d",&s[i],&s1[i],&s2[i]);
	}
	A=-100;
	for(i=0;i<=a;i++){
		for(j=0;j<b;j++){
			as[i][j]=A;
		}
	}
	for(i=1;i<=a;i++){
		K=0;
		G=0;
		for(j=0;j<b;j++){
			if(s[j]<=x[i]&&s1[j]>=x[i]){
				as[i][j]=s2[j];
				for(k=0;k<b;k++){
					if(as[i-1][k]!=A){
						G=as[i][j]-as[i-1][k];
						if(G<0){
							G=G*-1;
						}
						if(K<G){
							K=G;
						}
					}
				}
			}
		}
		M+=K;
	//	printf("%d\n",M);
	}
	printf("%d\n",M);
	return 0;
}