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<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
    int dp[210][2],T[210],strP[210][2],n,m,maxT,minT,P;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&T[i]);
        strP[i][0] = 0;
        strP[i][1] = 100;
    }
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d%d",&minT,&maxT,&P);
        for(int j = 0; j < n; j++)
        {
            if(minT <= T[j] && T[j] <= maxT)
            {
                strP[j][0] = max(strP[j][0],P);
                strP[j][1] = min(strP[j][1],P);
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            if(i == 0)dp[i][j] = 0;
            else
            {
                dp[i][j] = max(
                    dp[i - 1][j] + abs(strP[i - 1][j] - strP[i][j]),
                    dp[i - 1][(j + 1) % 2] + abs(strP[i - 1][(j + 1) % 2] - strP[i][j])
                );
            }
        }
    }
    /*for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < n; j++)
        {
            printf("%2d ",dp[j][i]);
        }
        printf("\n");
    }*/
    printf("%d\n",max(dp[n - 1][0],dp[n - 1][1]));
}