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++: //
//  main.cpp

#include <iostream>
#include <cmath.h>


using namespace std;
struct status{
	int toptemp;
	int bottomtemp;
	int fancy;
	
};
status dress[201];
int dp[201][201];//i??\???????????????j????????????????????§????´???????
int temp[201];
int D,N;

int MAX(int i,int j){//i??\?????????j???????????????????????§???????????????
	if(i == 0){//?????\????????????????????????
//		return dress[j].fancy;
		return 0;
	}
	
//	for(int i=0;i<N;i++){
	for(int J=0;J<N;J++){//i-1??\?????????J(1<D)???????????????????????§???
		if(temp[i-1] >= dress[J].bottomtemp && temp[i-1] <= dress[J].toptemp){
			dp[i][j] = max(dp[i][j],MAX(i-1,J) + abs(dress[J].fancy - dress[j].fancy));
		}
	}
	return dp[i][j];
//		dp[i-1][j]
//	}
}

int main(){	
	
	cin >> D >> N;
	
	
	for(int i=0;i<D;i++){
		cin >> temp[i];
	}
	for(int i=0;i<N;i++){
		cin >> dress[i].bottomtemp >> dress[i].toptemp >> dress[i].fancy;
	}
	

	for(int i=0;i<201;i++){
		for(int j=0;j<201;j++){
			dp[i][j] = -1;
		}
	}
	int answer=0;
	for(int i=0;i<N;i++){
		if(temp[D-1] >= dress[i].bottomtemp && temp[D-1] <= dress[i].toptemp){
			answer = max(answer,MAX(D-1,i));
		}
	}
	cout << answer << endl;
	
	/*
	for(int i=0;i<N;i++){//0??\?????????????¨???????
		if(temp[0] >= dress[i].bottomtemp && temp[0] <= dress[i].toptemp){
			dp[0][i] = dress[i].fancy;
		}
	}
	
	for(int i=1;i<D;i++){
		for(int j=0;j<N;j++){
			if(temp[i] >= dress[j].bottomtemp && temp[i] <= dress[j].toptemp){//i??\??????j??????????????????
				int Maxfancy=-1;
				for(int k=0;k<N;k++){//i-1??\????????¨????????????????????´???????????????????´???????????????§???????????????????????????
					if(dp[i-1][k] == -1)continue;
					Maxfancy = max(Maxfancy,abs(dress[j].fancy - dress[k].fancy));
				}
				dp[i][j] = Maxfancy;
			}
		}
	}
	
	int answer = 0;
	for(int i=1;i<D;i++){
		int Max = -1;
		for(int j=0;j<N;j++){
			Max = max(Max,dp[i][j]);
		}
		answer += Max;
	}
	cout << answer << endl;
	
	vector<int> dresses;
	int matrix[201][2];
	for(int i=0;i<D;i++){
		for(int j=0;j<N;j++){
			if(temp[i] >= dress[j].bottomtemp && temp[i] <= dress[j].toptemp){
				dresses.push_back(dress[j].fancy);
			}
		}
		sort(dresses.begin(),dresses.end());
		matrix[i][0] = dresses[0];
		matrix[i][1] = dresses[dresses.size() - 1];
	}
	
	int dp[201]={};
	dp[0] = max(matrix[0][0],matrix[0][1]);
	
	for(int i=1;i<D;i++){
		dp[i] = max(abs(dp[i-1] - matrix[i][0]),abs(dp[i-1] - matrix[i][1]));
	}
	
	cout << dp[D-1] << endl;
	*/
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
  int D, N;
  cin >> D >> N;
  vector<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];
  }

  vector<vector<int>> dp(D + 1, vector<int>(N + 1, -1));
  for (int i = 0; i < N; ++i) {
    if (A[i] <= T[0] && T[0] <= B[i]) {
      dp[1][i + 1] = 0;
    }
  }

  for (int i = 1; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      if (dp[i][j + 1] == -1) {
        continue;
      }

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

  int ans = -1;
  for (int i = 1; i <= N; ++i) {
    ans = max(dp[D][i], ans);
  }

  cout << ans << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int D, N, T[200], A[200], B[200], C[200], dp[200][200];
int rec(int today,int atai){
  if(today == D) return 0;
  if(dp[today][atai]) return dp[today][atai];
  int ret = 0;
  for(int i = 0 ; i < N ; i++ ){
    if(T[today] >= A[i] && T[today] <= B[i]){
      ret = max( ret, rec( today + 1, C[i]) + (today ? abs(atai - C[i]) : 0));
    }
  }
  return dp[today][atai] = ret;
}
int main(){
  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];
  cout << rec(0,0) << endl;
}

C++: #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
int main(){
	int d,n;
	int t[200] = {0};
	int a[200] = {0},b[200] = {0},c[200] = {0};
	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]);
	}
	int dp[201][201] = {{0}};
	for(int i = 0; i < d - 1; i++){
		for(int j = 0; j < n; j++){
			if(a[j] > t[i+1] || b[j] < t[i+1])continue;
			for(int k = 0; k < n; k++){
				if(a[k] > t[i] || b[k] < t[i])continue;
				int val = abs(c[j] - c[k]);
				dp[i+1][j] = max(dp[i+1][j],dp[i][k] + val);
			}
		}
	}
	int ans = -1;
	for(int i = 0; i < n; i++){
		ans = max(ans,dp[d-1][i]);
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define llong long long 
typedef struct data{
	int low;
	int high;
	int eva;
}data;
int D, N;
int temp[201];
data kind[200];
int score[201][200];

int solve(int d, int k, int e){
	//cout << d << " " << k << " " << e << "   " << score[d][k] << endl;
	score[d][k] = e;
	if(score[d][k] > e) return 0;
	if(d == D) return e;
	int ret = 0;
	for(int i = 0; i < N; i++){
		data use = kind[i];
		if(use.low > temp[d + 1] || use.high < temp[d + 1]) continue;
		if(d > 0) ret = max(ret, solve(d + 1, i, e + fabs(kind[k].eva - kind[i].eva)) );
		else ret = max(ret, solve(d + 1, i, 0));
	}

	return ret;
}

int main(){
	fill_n(temp, 201, -1);
	fill_n(*score, 201 * 200, 0);
	cin >> D >> N;
	for(int i = 1; i <= D; i++){
		cin >> temp[i];
	}
	for(int i = 0; i < N; i++){
		cin >> kind[i].low >> kind[i].high >> kind[i].eva;
	}

	cout << solve(0, 0, -1) << endl;
	return 0;
}

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

int main(){
	int D,N;
	int Kion[200];
	int Fmin[200],Fmax[200],Fhad[200];
	cin>>D>>N;
	for(int i=0;i<D;i++){
		cin>>Kion[i];
	}
	for(int i=0;i<N;i++){
		cin>>Fmin[i]>>Fmax[i]>>Fhad[i];
	}

	int Kekka[200][200];
	int rinji,rin2;
	//1??\???
	for(int i=0;i<N;i++){
		if(Kion[0]>=Fmin[i]&&Kion[0]<=Fmax[i]) Kekka[0][i]=0;
		else Kekka[0][i]=-1;
	}
	//2??\?????\???
	for(int day=1;day<D;day++){
		for(int fuku=0;fuku<N;fuku++){
			if(Kion[day]>=Fmin[fuku]&&Kion[day]<=Fmax[fuku]){
				rinji=-1;rin2=0;
				for(int mf=0;mf<N;mf++){
					if(Kekka[day-1][mf]!=-1&&rinji<=abs(Fhad[mf]-Fhad[fuku])) {rinji=abs(Fhad[mf]-Fhad[fuku]);rin2=Kekka[day-1][mf];}
				}
				Kekka[day][fuku]=rin2+rinji;
			}
			else{
				Kekka[day][fuku]=-1;
			}
		}
	}
	rinji=0;
	for(int i=0;i<N;i++){
		rinji=max(rinji,Kekka[D-1][i]);
	}
	cout<<rinji<<endl;
	return 0;
}