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 <algorithm>
#include <cstring>
using namespace std;
 
int main(){
    int d, n, t[201], a[201], b[201], c[201], dp[201][201], ans = 0;
    bool can[201][201];
 
    memset(dp, 0, sizeof dp);
    memset(can, false, sizeof can);
    cin >> d >> n;
    for(int i = 1; i <= d; i++){
        cin >> t[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> a[i] >> b[i] >> c[i];
    }
 
    for(int i = 1; i <= d; i++){
        for(int j = 1; j <= n; j++){
            if(a[j] <= t[i] && t[i] <= b[j]){
                can[i][j] = 1;
            }
        }
    }
 
 
    for(int i = 1; i <= d; i++){
        for(int j = 1; j <= n; j++){
            if(can[i][j]){
                for(int l = 1; l <= n; l++){
                    if(can[i - 1][l]){
                        int x = ((c[j] > c[l])? c[j] - c[l]: c[l] - c[j]);
                        dp[i][j] = max(dp[i][j], x + dp[i - 1][l]);
                    }
                }
            }
            if(i == d){
                ans = max(ans, dp[i][j]);
            }
        }
    }
 
    cout << ans << endl;
 
    return 0;
}

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

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> temp(N);
    for (auto& i : temp) cin >> i;
    vector<int> A(M), B(M), C(M);
    for (int i = 0; i < M; i++)
        cin >> A[i] >> B[i] >> C[i];

    int dp[N+1][M];
    for (int i = 0; i < N + 1; i++)
        for (int j = 0; j < M; j++) dp[i][j] = -100000;

    for (int i = 0; i < M; i++) {
        if (temp[0] < A[i] || B[i] < temp[0]) continue;
        dp[0][i] = 0;
    }

    for (int i = 1; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (temp[i] < A[j] || B[j] < temp[i]) continue;
            for (int k = 0; k < M; 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 < M; i++)
        ans = max(dp[N-1][i], ans);

    cout << ans << endl;

    return 0;
}

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

using namespace std;

int main(void){

  int d,n,t,a,b,c,dp[202][202];
  vector<int>T,A,B,C;

  cin >> d >> n;
  
  //末尾にtを追加していく
  for(int i=0;i<d;i++){
    scanf("%d",&t);
    T.push_back(t);
  }
  
  for(int i=0;i<n;i++){
    cin >> a >> b >> c;
    A.push_back(a);
    B.push_back(b);
    C.push_back(c);
  }
  
  //メモ用配列の初期化
  for(int i=0;i<202;i++)
    for(int j=0;j<202;j++)
      dp[i][j]=-1;
  
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      if(A[i]<=T[0] && T[0]<=B[i] && A[j]<=T[1] && T[1]<=B[j])
	dp[0][j]=max(dp[0][j],abs(C[i]-C[j]));
  
  for(int i=1;i<=d;i++)
    for(int j=0;j<n;j++)
      for(int k=0;k<n;k++)
	if(A[k]<=T[i+1] && T[i+1]<=B[k] && dp[i-1][j]>=0)
	  //メモしていく
	  dp[i][k]=max(dp[i][k],dp[i-1][j]+abs(C[j]-C[k]));
  
  int ans=0;
  for(int i=0;i<n;i++)
    ans=max(ans,dp[d-2][i]);
  
  printf("%d\n",ans);
  
  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;

  

}

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][200];

int solve(int d, int k, int e){
	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;
		ret = max(ret, solve(d + 1, i, e + fabs(kind[k].eva - kind[i].eva)) );
	}

	return ret;
}

int main(){
	fill_n(temp, 201, -1);
	fill_n(**score, 201 * 200 * 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<stdio.h>
#include<stdlib.h>
int s[200][200],r[200][200],t[200],a[200],b[200],c[200],d[200];
int main(void)
{
	int m,n,i,k,h,x;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++) scanf("%d",&t[i]);
	for(i=0;i<m;i++) scanf("%d %d %d",&a[i],&b[i],&c[i]);
	for(i=0;i<n;i++){
		for(k=0;k<m;k++) s[i][k]=0;
	}
	for(i=0;i<n;i++){
		d[i]=0;
		for(k=0;k<m;k++){
			if(a[k]<=t[i]&&t[i]<=b[k]){
				r[i][d[i]]=c[k];
				d[i]++;
			}
			
		}
	}
	for(i=1;i<n;i++){
		for(k=0;k<d[i];k++){
			for(h=0;h<d[i-1];h++){

				if(s[i][k]<abs(r[i][k]-r[i-1][h])+s[i-1][h]){
					s[i][k]=abs(r[i][k]-r[i-1][h])+s[i-1][h];
				}			
			}
		}
	}
	x=0;
	for(i=0;i<d[n-1];i++){
		if(x<s[n-1][i]) x=s[n-1][i];
	}
	printf("%d\n",x);
	return 0;
}

C++: #include<iostream>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int d,n;
	cin>>d>>n;
	int t[d],a[n],b[n],c[n],dp[d][n];
	rep(i,d)cin>>t[i];
	rep(i,n)cin>>a[i]>>b[i]>>c[i];
	rep(i,d)rep(j,n)dp[i][j]=-1;
	rep(i,n)dp[0][i]=0;
	rep(i,d-1){
		rep(j,n){
			rep(k,n)if(a[j]<=t[i] && t[i]<=b[j] && a[k]<=t[i+1] && t[i+1]<=b[k])dp[i+1][k]=max(dp[i+1][k],dp[i][j]+(int)abs(c[k]-c[j]));
		}
	}
	int ans=0;
	rep(i,n)ans=max(ans,dp[d-1][i]);
	cout<<ans<<endl;
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int dp[201][201];
int t[201],u[201],v[201],w[201];
int N,D,ans=0;
int main(){
  cin>>N>>D;
  for(int i=1;i<=N;i++)cin>>t[i];
  for(int i=1;i<=D;i++){
    cin>>u[i]>>v[i]>>w[i];
    if(t[1]<u[i]||v[i]<t[1])dp[1][i]=-1;
    else dp[1][i]=0;
  }
  for(int i=2;i<=N;i++){
    for(int j=1;j<=D;j++){
      dp[i][j]=-1;
      if(t[i]<u[j]||v[j]<t[i])continue;
      for(int k=1;k<=D;k++){
	if(dp[i-1][k]==-1)continue;
	dp[i][j]=max(dp[i][j],dp[i-1][k]+abs(w[k]-w[j]));
      }
      ans=max(ans,dp[i][j]);
    }
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
int main()
{
	int n, m, t[1001], c[100],c2[101],v[100],dp[1001][100];
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> t[i];
	for (int i = 1; i <= m; i++){
		cin >> c[i] >> c2[i] >> v[i];
	}
	memset(dp, -1, sizeof(dp));

	for (int i = 1; i <= m; i++){
		if (t[1] >= c[i] && t[1]<=c2[i])
			dp[1][i] = 0;
	}

	for (int i = 1; i <= n; i++){
		for (int i2 = 1; i2 <= m; i2++){
			if (dp[i][i2] >= 0){
				for (int i3 = 1; i3 <= m; i3++){
					if (t[i + 1] >= c[i3] && t[i + 1] <= c2[i3]){
						dp[i + 1][i3] = max(dp[i][i2] + abs(v[i2] - v[i3]), dp[i][i3]);
					}
				}
			}
		}
	}
		int maxv=0;
		for (int i = 0; i < m; i++){
			maxv = max(maxv, dp[n][i]);
		}
		cout << maxv << endl;
}