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)
}

Python: def solve():
    from sys import stdin
    f_i = stdin
    
    D, N = map(int, f_i.readline().split())
    T = tuple(int(f_i.readline()) for i in range(D))
    
    clothes = [tuple(map(int, f_i.readline().split())) for i in range(N)]
    clothes.sort(key=lambda x: x[2])
    
    candidate = []
    for t in T:
        ct = tuple(filter(lambda x: x[0] <= t <= x[1], clothes))
        c_min = ct[0][2]
        c_max = ct[-1][2]
        if c_min == c_max:
            candidate.append((c_min,))
        else:
            candidate.append((c_min, c_max))
    
    dp = [0] * len(candidate[0])
    for c1, c2 in zip(candidate, candidate[1:]):
        dp = [max(rec + abs(ci - cj) for rec, ci in zip(dp, c1)) for cj in c2]
    
    print(max(dp))
    
solve()

Python: d,n=map(int,input().split())
t=[int(input()) for _ in [0]*d]
abc=[list(map(int,input().split())) for _ in [0]*n]

memo=[-1]*101

t_list=[set() for _ in [0]*61]

for a,b,c in abc:
    for j in range(a,b+1):
        t_list[j].add(c)
        
for i in t_list[t[0]]:
    memo[i]=0
    
for i in t[1:]:
    memo2=[-1]*101
    temp=[j for j in t_list[i]]
    for j,k in enumerate(memo):
        if k!=-1:
            for l in temp:
                memo2[l]=max(memo2[l],k+abs(j-l))
    memo=memo2
    
print(max(memo))

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

typedef long long ll;

using namespace std;


int main(){
    int num[200][3],n,sum;
    int a[101][3]={0};
    
    scanf("%d",&n);
    
    for(int i=0;i<n;i++)
        for(int j=0;j<3;j++)
            scanf("%d",&num[i][j]);
    
    for(int i=0;i<3;i++)
        for(int j=0;j<n;j++)
            a[num[j][i]][i]++;
    
    for(int i=0;i<n;i++){
        sum=0;
        for(int j=0;j<3;j++)
            if(a[num[i][j]][j]<2)sum+=num[i][j];
        printf("%d\n",sum);
    }
    
    return 0;
}

C++:     return 0;
  } 
  return pointM;//自分の今までの派手さ絶対値の合計（今日ー昨日まで）
}



int main(void){
 
  cin >> days;
  cin >> clothes;
  for(int i=0;i<days;i++){
    cin >> temple[i];
  }
  for(int i=0;i<clothes;i++){
    cin >> clothD[i][0];
    cin >> clothD[i][1];
    cin >> clothD[i][2];
  }
  for(int i=0;i<days;i++){
	dayCanW[i]=0;
    for(int j=0;j<clothes;j++){
	dayM[i][j]=0;
    }
  }
  /*
  for(int i=0;i<days;i++){
    for(int j=0;j<clothes;j++){
      if(temple[i]>clothD[j][0] && temple[i]<clothD[j][1]){
	dayM[i][j]=1;
	dayCanW[i]+=1;
      }
    }
  }
  */
  cout << bestCloth(0,0) << endl;

  return 0;

}

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

using namespace std;

int D, N;
vector<int> MaxTempInfo;
vector<pair<int, int> > ColthesTempInfo;
vector<int> ColorInfo;
int DpColor[201][201];

void solve() {
	int curTemp, minTemp, maxTemp;

	fill(DpColor[0], DpColor[0] + 201 * 201, -1);
	curTemp = MaxTempInfo[0];
	for (int j = 1; j <= N; ++j) {
		minTemp = ColthesTempInfo[j - 1].first;
		maxTemp = ColthesTempInfo[j - 1].second;
		if (curTemp < minTemp || curTemp > maxTemp) {
			continue;
		}
		DpColor[1][j] = 0;
	}
	for (int i = 2; i <= D; ++i) {
		curTemp = MaxTempInfo[i - 1];
		for (int j = 1; j <= N; ++j) {
			minTemp = ColthesTempInfo[j - 1].first;
			maxTemp = ColthesTempInfo[j - 1].second;
			if (curTemp < minTemp || curTemp > maxTemp) {
				continue;
			}
			for (int k = 1; k <= N; ++k) {
				if (DpColor[i - 1][k] == -1) {
					continue;
				}
				DpColor[i][j] = max(DpColor[i][j], DpColor[i - 1][k] + abs(ColorInfo[j - 1] - ColorInfo[k - 1]));
			}
		}
	}
	
	cout << *max_element(DpColor[D], DpColor[D] + N) << endl;
}

int main() {
	int temp, a, b, c;

	cin >> D >> N;
	for (int i = 0; i < D; ++i) {
		cin >> temp;
		MaxTempInfo.push_back(temp);
	}
	for (int i = 0; i < N; ++i) {
		cin >> a >> b >> c;
		ColthesTempInfo.push_back({ a,b });
		ColorInfo.push_back(c);
	}
	solve();
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[110][110],l[110],r[110],p[110],n,m,a[110];
int main(){
	cin>>n>>m;for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++)cin>>l[i]>>r[i]>>p[i];
	for(int i=0;i<12100;i++)dp[i/110][i%110]=-1000000000;
	for(int i=0;i<m;i++){if(l[i]<=a[0] && a[i]<=r[0]){dp[0][p[i]]=max(dp[0][p[i]],0);)}}
	for(int i=1;i<n;i++){
		for(int j=0;j<=100;j++){
			for(int k=0;k<m;k++){
				if(l[k]<=a[i] && a[i]<=r[k]){
					dp[i][p[k]]=max(dp[i][p[k]],dp[i-1][j]+abs(j-p[k]));)
				}
			}
		}
	}
	int maxn=0;for(int i=0;i<=100;i++)maxn=max(maxn,dp[n-1][i]);
	cout<<maxn<<endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

struct cloth{int a,b,c;};
const int MAX = 205;
vector<cloth> V;
vector<int> Temp;
int N,D,dp[MAX][MAX];

void input(){
  cin >> D >> N;
  Temp.resize(D);
  V.resize(N);
  for(int i = 0; i < D; i++) cin >> Temp[i];
  for(int i = 0; i < N; i++) cin >> V[i].a >> V[i].b >> V[i].c;
}

bool isValid(int temp, cloth c){
  return c.a <= temp && temp <= c.b;
}

void solve(){
  memset(dp,-1,sizeof(dp));

  for(int i = 0; i < V.size(); i++){
    if(isValid(Temp[0],V[i])) dp[0][i] = 0;
  }

  for(int i = 0; i < D-1; i++){
    for(int j = 0; j < V.size(); j++)
      for(int k = 0; k < V.size(); k++){
	if(dp[i][j] == -1) continue;
	if(!isValid(Temp[i+1],V[k])) continue;
	int diff = abs(V[k].c - V[j].c);
	dp[i+1][k] = max(dp[i+1][k],dp[i][j]+diff);
      }
  }
  int ans = 0;

  for(int i = 0; i < V.size(); i++) ans = max(ans,dp[D-1][i]);
  cout << ans << endl;
}

int main(){
  input();
  solve();
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    int D, N;
    cin >> D >> N;
    int T[D], A[N], B[N], C[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];
    }
    int x[D][2];
    for (int i = 0; i < D; i++) {
        x[i][0] = 101;
        x[i][1] = -1;
    }
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < N; j++) {
            if (A[j] <= T[i] && T[i] <= B[j]) {
                x[i][0] = min(x[i][0], C[j]);
                x[i][1] = max(x[i][1], C[j]);
            }
        }
    }
    int dp[D][2]{};
    for (int i = 0; i < D - 1; i++) {
        dp[i + 1][0] = max(abs(x[i + 1][0] - x[i][0]) + dp[i][0], abs(x[i + 1][0] - x[i][1]) + dp[i][1]);
        dp[i + 1][1] = max(abs(x[i + 1][1] - x[i][0]) + dp[i][0], abs(x[i + 1][1] - x[i][1]) + dp[i][1]);
    }
    cout << max(dp[D - 1][0], dp[D - 1][1]) << "\n";
    return 0;
}