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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct status{
	int toptemp;
	int bottomtemp;
	int fancy;
	
};

int main(){	
	int D,N;
	cin >> D >> N;
	status dress[201];
	int temp[201];
	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;
	}
	
	int dp[201][201];//i??\???????????????j????????????????????§????´???????
	for(int i=0;i<201;i++){
		for(int j=0;j<201;j++){
			dp[i][j] = -1;
		}
	}
	
	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 d, n;
int t[202];
int a[202], b[202], c[202];

int memo[256][256];

int dfs(int i, int pre)
{
  if (i == d - 1) {
    int maxi = 0;
    for (int x = 0; x < n; x++) {
      if (a[x] <= t[i] && t[i] <= b[x]) {
        maxi = max(maxi, abs(c[pre] - c[x]));
      }
    }
    return maxi;
  }
  int& maxi = memo[i][pre];
  if (maxi != -1) return maxi;
  maxi = 0;
  for (int x = 0; x < n; x++) {
    if (a[x] <= t[i] && t[i] <= b[x]) {
      maxi = max(maxi, dfs(i + 1, x) + abs(c[pre] - c[x]));
    }
  }
  return maxi;
}

int main()
{
  memset(memo, -1, sizeof(memo));
  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];
  }

  int maxi = 0;
  for (int i = 0; i < n; i++){
    if (a[i] <= t[0] && t[0] <= b[i]) {
      maxi = max(maxi, dfs(1, i));
    }
  }

  cout << maxi << endl;
}

C++: // JOI13年予選４　暑い日々
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0579
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main(void) {
    int D,N;
    int t[201],a[201],b[201],c[201];
    int DP[201][201];
    cin >> D >> N;
    for(int j=1; j<=D; j++) {
        cin >> t[j];
    }
    for(int i=1; i<=N; i++) {
        cin >> a[i] >> b[i] >> c[i];
    }
    for(int i=1;i<=N;i++){
        if(a[i]<=t[1] && t[1]<=b[i]){
            DP[i][1]=0;
        }
        else{
            DP[i][1]=-1;
        }
    }
    for(int j=2;j<=D;j++){
        for(int i=1;i<=N;i++){
            if(a[i]<=t[j] && t[j]<=b[i]){
                int tempmax=0;
                for(int pi=1;pi<=N;pi++){
                    if(DP[pi][j-1]>=0){
                        if(tempmax < abs(c[i]-c[pi])+DP[pi][j-1]){
                            tempmax = abs(c[i]-c[pi])+DP[pi][j-1];
                        }
                    }
                }
                DP[i][j]=tempmax;
            }
            else{
                DP[i][j]=-1;
            }
        }
    }

int max=0;
for(int i=1;i<=N;i++){
if(max<DP[i][D])max=DP[i][D];
}
cout << max << endl;
    return 0;
}

C++: 
//13-4 暑い日々
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<stack>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<random>
#include<cassert>
using namespace std;
#define LL long long
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define segment_size 65536
#define ROOP() while(true)

int D,N;
int T[200];
int A[200];
int B[200];
int C[200];
vector<int> s[61];//o度の日に着られる服一覧

int dp[200][101];

int cul(int now, int before){
    if(dp[now][before]!=INT_MIN) return dp[now][before];
    if(now==D-1){
        int tmp = 0;
        for(int i=0; i<s[T[now]].size(); i++) tmp = max(tmp, abs(before-s[T[now]].at(i)));
        dp[now][before] = tmp;
        return tmp;
    }
    int re = 0;
    for(int i=0; i<s[T[now]].size(); i++){
        re = max(re, cul(now+1, s[T[now]].at(i)) + abs(before-s[T[now]].at(i)) );
    }
    dp[now][before] = re;
    return re;
}

int main(){
    cin >> D >> N;
    for(int i=0; i<D; i++){
        cin >> T[i];
        for(int j=0; j<101; j++){
            dp[i][j] = INT_MIN;
        }
    }
    for(int i=0; i<N; i++){
        cin >> A[i] >> B[i] >> C[i];
        for(int j=A[i]; j<=B[i]; j++){
            s[j].push_back(C[i]);
        }
    }
    int ans = 0;
    for(int i=0; i<s[T[0]].size(); i++){
        ans = max(ans, cul(1,s[T[0]].at(i)));
    }
    cout << ans << endl;
    
    return 0;
}

C++: 
#include <iostream>
#include <cstdlib>
#include<cmath>
#include <math.h>
using namespace std;

int main(){
	int D,N,T[200],A[200][3];
	int dp[200][101];
	cin >> N >> D ;
	for(int i=0;i<N;i++){
		cin >>T[i];
	}
	for(int i=0;i<D;i++){
		for(int j=0;j<3;j++){
			cin >>A[i][j];
		}
	}
	for(int i=0;i<N;i++){
		for(int j=0;j<D;j++){
			dp[i][j]=-9999;
		}
	}
	for(int l=0;l<D;l++){
		if(T[0]>A[l][0] && T[0]<A[l][1] ){
			dp[ 0 ][ A[l][2] ]=0;
			

		}
	}
	for(int i=1;i<N;i++){
		for(int j=0;j<D;j++){
			if(T[i]>=A[j][0] && T[i]<=A[j][1] ){
				for(int l=0;l<=100;l++){
					if(dp[i-1 ][ l ]>=0)
						dp[ i ][ A[j][2] ]=max(dp[ i ][ A[j][2] ],dp[ i-1 ][ l ]+abs(l-A[j][2]));//テ、ツサツ甘」ツ?セテ」ツ?ァテ」ツ?ョテヲツ慊?ゥツォツ佚」ツ??、ツサツ甘・ツ崢?
					

				}
			}
		}
	}
	int answer=0;
	for(int j=0;j<101;j++)
	{
		answer=max(answer,dp[ N-1 ][j]);
	}
	cout << answer << endl;
	return 0;
}