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>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
static const LL INF = 1<<60;
static const int MAX_N = 300;
using namespace std;

struct clothes{
    LL A[MAX_N],B[MAX_N],C[MAX_N];
};

LL mem[MAX_N][MAX_N];
int N,D;
int Day[MAX_N];
LL ans;
clothes T;

LL cal(int day,int pp){
   LL res=0;
   if(day==N+1){
        return 0;
   }
   if(mem[day][pp]!=-1)return mem[day][pp];
   else {
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]){
                if(day<2)mem[day][pp]=res=cal(day+1,T.C[i]);
                else {
                    mem[day][pp]=res=max(cal(day+1,T.C[i])+abs(pp-T.C[i]),res);
                }
            }
        }
   }
   return mem[day][pp]=res;
}

int main(){
    cin>>N>>D;
    fill(mem[0],mem[MAX_N],-1);
    FOR(i,1,N){
        cin>>Day[i];
    }
    FOR(i,1,D){
        cin>>T.A[i]>>T.B[i]>>T.C[i];
    }
    cout<<cal(1,0)<<endl;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
# define  max 200
int d,n;
int t[max];
int a[max],b[max],c[max];
bool suit[max][max];
int dp[max][max];

int main(){
  scanf("%d %d",&d,&n);
  for(int i=0;i<d;i++){
    scanf("%d",&t[i]);
  }
  for(int j=0;j<n;j++){
    scanf("%d %d %d",&a[j],&b[j],&c[j]);
  }
  for(int i=0;i<d;i++){
    for(int j=0;j<n;j++){
      suit[i][j]=(t[i]>=a[j]&&t[i]<=b[j]);
    }
  }
   for(int i=0;i<d;i++){
   	for(int j=0;j<n;j++){
   		dp[i][j]=-1;
   	}
   }
   for(int j=0;j<n;j++){
   	  if (suit[0][j]==true) {
		dp[0][j]=0;
	}
   }
   for(int i=0;i<d-1;i++){
   	for(int j=0;j<n;j++){
   		if(dp[i][j]>=0){
   			for(int k=0;k<n;k++){
   				if(suit[i+1][k]==true){
   				  if(dp[i+1][k]<dp[i][j]+abs(c[j]-c[k])){
   				  	dp[i+1][k]=dp[i][j]+abs(c[j]-c[k]);
   				  }	
   				}
   			}
   		}
   		}
   	}
   	int ans=-1;
  for(int i=0;i<n;i++){
  	if(ans<dp[d-1][i]){
  		ans=dp[d-1][i];
  	}
  }
  printf("%d\n",ans);
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long lint;

int main()
{
	int n, d;
	int dp[256][256]; //i日目に服jを着た時の最大値
	int heat[256];
	int mi[256], ma[256], val[256];
	
	memset(dp, 0, sizeof(dp));
	
	scanf("%d %d", &d, &n);
	
	for (int i = 1; i <= d; i++){
		scanf("%d", heat + i);
	}
	
	for (int i = 0; i < n; i++){
		scanf("%d %d %d", mi + i, ma + i, val + i);
	}
	
	for (int i = 2; i <= d; i++){
		for (int j = 0; j < n; j++){
			if (!(mi[j] <= heat[i] && heat[i] <= ma[j])) continue;
			for (int k = 0; k < n; k++){
				if (!(mi[k] <= heat[i - 1] && heat[i - 1] <= ma[k])) continue;
				dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(val[j] - val[k]));
			}
		}
	}
	
	int ans = -1;
	for (int i = 0; i < n; i++) ans = max(ans, dp[d][i]);
	
	printf("%d\n", ans);
	
	return (0);
}

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

using namespace std;

int t[200];
int a[200], b[200], c[200];
int dp[200][200];

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

	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<stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
    int dp[210][2],T[210],strP[210][2],n,m,maxT,minT,P;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&T[i]);
        strP[i][0] = 0;
        strP[i][1] = 100;
    }
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d%d",&minT,&maxT,&P);
        for(int j = 0; j < n; j++)
        {
            if(minT <= T[j] && T[j] <= maxT)
            {
                strP[j][0] = max(strP[j][0],P);
                strP[j][1] = min(strP[j][1],P);
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            if(i == 0)dp[i][j] = 0;
            else
            {
                dp[i][j] = max(
                    dp[i - 1][j] + abs(strP[i - 1][j] - strP[i][j]),
                    dp[i - 1][(j + 1) % 2] + abs(strP[i - 1][(j + 1) % 2] - strP[i][j])
                );
            }
        }
    }
    /*for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < n; j++)
        {
            printf("%2d ",dp[j][i]);
        }
        printf("\n");
    }*/
    printf("%d\n",max(dp[n - 1][0],dp[n - 1][1]));
}

C++: #include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int D,N;

    cin >> D >> N;

    int T[D];
    int MN[D],MX[D];

    for(int i=0; i<D; ++i) cin >> T[i];

    int A[N],B[N],C[N];

    for(int i=0; i<N; ++i) cin >> A[i] >> B[i] >> C[i];


    for(int i=0; i<D; ++i) {
        MN[i]=101; MX[i]=-1;
        for(int j=0; j<N; ++j) {
            if(A[j]<=T[i] && T[i]<=B[j]) {
                MN[i]=min(MN[i],C[j]);
                MX[i]=max(MX[i],C[j]);
            }
        }
    }

    int dp[2][D];

    dp[0][0]=0;
    dp[1][0]=0;

    for(int i=1; i<D; ++i) {
        dp[0][i]=max(dp[0][i-1]+abs(MN[i-1]-MN[i]),dp[1][i-1]+abs(MX[i-1]-MN[i]));
        dp[1][i]=max(dp[0][i-1]+abs(MN[i-1]-MX[i]),dp[1][i-1]+abs(MX[i-1]-MX[i]));
    }

    cout << max(dp[0][D-1],dp[1][D-1]) << endl;

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#define max(a,b) ((a)>=(b)?(a):(b))
#define abs(x) ((x)>=0?(x):-(x))
int dp[200][200],A[200],B[200],C[200],T[200];
int main() {
	int D,N;
	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]);
	memset(dp,0,sizeof(dp));
	for(int i=1;i<D;i++) for(int j=0;j<N;j++) {
		if(A[j]<=T[i] && T[i] <=B[j]) {
			for(int k=0;k<N;k++) {
				if(A[k]<=T[i-1] && T[i-1]<=B[k]) {
					dp[i][j]=max(dp[i][j],dp[i-1][k]+abs(C[j]-C[k]));
				}
			}
		}
	}
	int ans=-1;
	for(int i=0;i<N;i++) ans=max(ans,dp[D-1][i]);
	printf("%d\n",ans);
}

C++: #include<iostream>
#include<complex>
using namespace std;
int dp[200][200],h[200],a[200],b[200],c[200];
int main(){
	int d,n;
	cin>>d>>n;
	for(int i=0;i<d;i++){cin>>h[i];for(int j=0;j<n;j++)dp[i][j]=0;}
	for(int i=0;i<n;i++){cin>>a[i]>>b[i]>>c[i];}
	for(int i=1;i<d;i++)for(int j=0;j<n;j++)if(h[i]>=a[j]&&h[i]<=b[j])for(int k=0;k<n;k++){if(h[i-1]>=a[k]&&h[i-1]<=b[k])dp[i][j]=max(dp[i][j],abs(c[j]-c[k])+dp[i-1][k]);}
	int ans=0;
	for(int i=0;i<n;i++)ans=max(ans,dp[d-1][i]);
	cout<<ans<<endl;
	return 0;
}