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 := 1; 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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;

ll n, d, p = 0;
ll t[1000], a[1000], b[1000], c[1000];
vector< pair<int,int> > vpi;

int main(){
	cin >> n >> d;
	REP(i, n){
		cin >> t[i];
	}
	REP(i, d){
		cin >> a[i] >> b[i] >> c[i];
	}
	REP(i, n){
		ll ma = -1, mi = 10000;
		REP(j, d){
			if (t[i] >= a[j] && t[i] <= b[j]){
				ma = max(c[j], ma);
				mi = min(c[j], mi);
			}
		}
		vpi.push_back(make_pair(ma, mi));
	}
	REP(i, vpi.size() - 1){
		int a = abs(vpi[i + 1].first - vpi[i].first);
		a = max(a, abs(vpi[i + 1].second - vpi[i].first));
		a = max(a, abs(vpi[i + 1].first - vpi[i].second));
		a = max(a, abs(vpi[i + 1].second - vpi[i].second));
		p += a;
	}
	cout << p << endl;
}

C++: //　暑い日々　0579
#include<bits/stdc++.h>
using namespace std;
int dp[201][201];
int main(void)
{
	int d,n,t[201],a[201],b[201],c[201];
	int i,j,k,w=0,x,max;
	
	cin>>d>>n;
	for(i=1;i<=d;i++) cin>>t[i];
	for(i=1;i<=n;i++) cin>>a[i]>>b[i]>>c[i];
	
	for(i=1;i<=d;i++){
		for(j=1;j<=n;j++) dp[i][j]=-1;
	}
	
	for(i=1;i<=n;i++){
			if(t[1]<=b[i]&&t[1]>=a[i]) dp[1][i]=0;
	}
	
	for(i=1;i<d;i++){
		for(j=1;j<=n;j++){
			if(dp[i][j]!=-1){
				for(k=1;k<=n;k++){
					if(t[i+1]>=a[k]&&t[i+1]<=b[k]){
							w=abs(c[j]-c[k]);	
						if(dp[i+1][k]<dp[i][j]+w){
							dp[i+1][k]=dp[i][j]+w;
						}	
					}
				}
			}
		}
	}
	
	x=-1;
	for(i=1;i<=n;i++){
		if(x<dp[d][i]) x=dp[d][i];
	}
	
	cout<<x<<endl;
	
	return 0;
}

C++: #include<bits/stdc++.h>
#define LL long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)

using namespace std;

static const LL MAX_N = 1e+5;
static const LL INF = 1 << 25;

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

int N,D;
LL res=-INF;
LL Day[MAX_N];
LL mem[300][110][110];
clothes T;

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


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

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<cmath>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> Pi;
int D,N;

int f(vector<vector<int> >& list,vector<vector<int> >& used)
{
  
  int mex = 0;
  queue<Pi> que;// Pi(P(value,day),pre_index)
  for(int i=0;i<list[0].size();i++)
    {
      que.push(Pi(P(0,1),i));
      used[0][i] = 0;
    }
  
  while(!que.empty())
    {
      Pi p = que.front(); que.pop();
      int total,day,pre_index;
      total = p.F.F, day = p.F.S, pre_index = p.S;

      if(day >= D)
	{
	  mex = max(mex,total);
	  continue;
	}

      for(int i=0;i<list[day].size();i++)
	{
	  int sum = total + abs(list[day-1][pre_index]-list[day][i]);
	  if(used[day][i] >= sum)
	    continue;

	  used[day][i] = sum;
	  que.push(Pi(P(sum,day+1),i));
	}

    }

  return mex;
}


int main()
{

  cin >> D >> N;
  vector<int> T;
  vector<vector<int> > list,used;
  list.resize(D),used.resize(D);


  for(int i=0;i<D;i++)
    {
      int in; cin >> in;
      T.push_back(in);
    }

  for(int j=0;j<N;j++)
    {
      int A,B,C;
      cin >> A >> B >> C;
      for(int k=0;k<D;k++)
	{
	  if(A <= T[k] && T[k] <= B)
	    {
	      list[k].push_back(C),used[k].push_back(-1);
	    }
	}
    }
  cout << f(list,used) << endl;


  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
const int A=0;
const int B=1;
const int C=2;
const int INF=45451919;
int D,N;
int temp[201],huku[201][3];
int dp[201][201];


int main(){
	cin>>D>>N;
	for(int i=1;i<=D;i++)cin>>temp[i];
	for(int i=1;i<=N;i++)cin>>huku[i][A]>>huku[i][B]>>huku[i][C];
	//for(int i=1;i<=N;i++)cout<<huku[i][A]<<huku[i][B]<<huku[i][C]<<endl;
	for(int i=0;i<201;i++)for(int j=0;j<201;j++)dp[i][j]=-1;
	for(int i=1;i<=N;i++)if(huku[i][A]<=temp[1]&&huku[i][B]>=temp[1])dp[1][i]=0;
	
	for(int i=1;i<10;i++){
	for(int j=0;j<10;j++)
	cout<<dp[i][j]<<" ";cout<<endl;
	}
	
for(int i=1;i<=D;i++){
	for(int j=1;j<=N;j++){
		for(int k=1;k<=N;k++){
			if(huku[k][A]<=temp[i+1]&&huku[k][B]>=temp[i+1]&&dp[i][k]!=0){
				dp[i+1][j]=max(abs(huku[k][C]-huku[j][C]),dp[i+1][j]);
				}
		}
	}
}
	//cout<<endl<<dp[2][1]<<endl;
	for(int i=1;i<=D;i++){
		for(int j=1;j<=N;j++){
			cout<<dp[i][j]<<" ";
		}
			cout<<endl;
	}
	
return 0;
}

C++: //AOJ 0579: Hot days
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX_N = 100, INF = 10000000;
int n, d, ans, c[200][61], save[3], dt[200], plan[200][2], dp[200][2];
void solve(){
	for (int i = 0; i < d; i++){
		for (int j = 0; j < n; j++){
			if (c[j][dt[i]] != -1){
				plan[i][0] = min(c[j][dt[i]], plan[i][0]);
				plan[i][1] = max(c[j][dt[i]], plan[i][1]);
			}
		}
	}
	for (int i = 1; i < d; i++){
		dp[i][0] = abs(plan[i][0] - plan[i - 1][1]) + dp[i - 1][1];
		dp[i][1] = abs(plan[i][1] - plan[i - 1][0]) + dp[i - 1][0];
	}
}
int main(){
	cin >> d >> n;
	for (int i = 0; i < d; i++){
		cin >> dt[i];
	}
	for (int i = 0; i<n; i++){
		for (int j = 0; j<61; j++){
			c[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++){
		cin >> save[0] >> save[1] >> save[2];
		for (int j = save[0]; j <= save[1]; j++){
			c[i][j] = save[2];
		}
	}
	for (int i = 0; i<d; i++){
		plan[i][0] = INF;
	}
	solve();
	ans = max(dp[d - 1][0], dp[d - 1][1]);
	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;
}