using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4 4");
WillReturn.Add("10 8 12 5");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("2 3");
WillReturn.Add("3 4");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 1");
WillReturn.Add("0 10");
WillReturn.Add("1 2");
//0
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
// 隣接リスト
static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();
static int[] mHArr;
// 上りを正のコスト、下りをコスト0とした辺情報
struct EdgeInfoDef
{
internal int ToNode;
internal int Cost;
}
static Dictionary<int, List<EdgeInfoDef>> mEdgeInfoListDict = new Dictionary<int, List<EdgeInfoDef>>();
static void Main()
{
List<string> InputList = GetInputList();
mHArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
foreach (string EachStr in InputList.Skip(2)) {
SplitAct(EachStr);
int FromNode = wkArr[0];
int ToNode = wkArr[1];
if (mToNodeListDict.ContainsKey(FromNode) == false) {
mToNodeListDict[FromNode] = new List<int>();
}
if (mToNodeListDict.ContainsKey(ToNode) == false) {
mToNodeListDict[ToNode] = new List<int>();
}
mToNodeListDict[FromNode].Add(ToNode);
mToNodeListDict[ToNode].Add(FromNode);
}
// 上りを正のコスト、下りをコスト0とした辺情報を作成
foreach (var EachPair in mToNodeListDict) {
int FromNode = EachPair.Key;
foreach (int EachToNode in EachPair.Value) {
int FromHeight = mHArr[FromNode - 1];
int ToHeight = mHArr[EachToNode - 1];
EdgeInfoDef WillAdd;
WillAdd.ToNode = EachToNode;
WillAdd.Cost = 0;
if (FromHeight < ToHeight) {
WillAdd.Cost = ToHeight - FromHeight;
}
if (mEdgeInfoListDict.ContainsKey(FromNode) == false) {
mEdgeInfoListDict[FromNode] = new List<EdgeInfoDef>();
}
mEdgeInfoListDict[FromNode].Add(WillAdd);
}
}
Dijkstra();
}
//ダイクストラ法で、各ノードまでの最短距離を求める
static void Dijkstra()
{
var InsPQueue = new PQueue();
// 距離合計[確定ノード]なDict
var KakuteiNodeDict = new Dictionary<int, long>();
KakuteiNodeDict.Add(1, 0);
//Enqueue処理
Action<int> EnqueueAct = pFromNode =>
{
if (mEdgeInfoListDict.ContainsKey(pFromNode) == false) {
return;
}
foreach (EdgeInfoDef EachEdge in mEdgeInfoListDict[pFromNode]) {
// 確定ノードならContinue
if (KakuteiNodeDict.ContainsKey(EachEdge.ToNode)) continue;
long wkSumCost = KakuteiNodeDict[pFromNode] + EachEdge.Cost;
PQueue.PQueueJyoutaiDef WillEnqueue;
WillEnqueue.Node = EachEdge.ToNode;
WillEnqueue.SumCost = wkSumCost;
InsPQueue.Enqueue(WillEnqueue);
}
};
EnqueueAct(1);
while (InsPQueue.IsEmpty() == false) {
PQueue.PQueueJyoutaiDef Dequeued = InsPQueue.Dequeue();
//確定ノードならContinue
if (KakuteiNodeDict.ContainsKey(Dequeued.Node)) continue;
KakuteiNodeDict.Add(Dequeued.Node, Dequeued.SumCost);
EnqueueAct(Dequeued.Node);
}
// 終点ノードごとのスコアを求めてから、解を求める
var AnswerList = new List<long>();
foreach (var EachPair in KakuteiNodeDict) {
long AnswerKouho = mHArr[0] - mHArr[EachPair.Key - 1];
AnswerKouho -= EachPair.Value;
AnswerList.Add(AnswerKouho);
}
Console.WriteLine(AnswerList.Max());
}
}
#region PQueue
// 優先度付きキュー
internal class PQueue
{
internal struct PQueueJyoutaiDef
{
internal int Node;
internal long SumCost;
}
private Dictionary<int, PQueueJyoutaiDef> mHeapDict = new Dictionary<int, PQueueJyoutaiDef>();
internal bool IsEmpty()
{
return mHeapDict.Count == 0;
}
// エンキュー処理
internal void Enqueue(PQueueJyoutaiDef pAddJyoutai)
{
int CurrNode = 1 + mHeapDict.Count;
mHeapDict[CurrNode] = pAddJyoutai;
while (1 < CurrNode && mHeapDict[CurrNode / 2].SumCost > mHeapDict[CurrNode].SumCost) {
PQueueJyoutaiDef Swap = mHeapDict[CurrNode];
mHeapDict[CurrNode] = mHeapDict[CurrNode / 2];
mHeapDict[CurrNode / 2] = Swap;
CurrNode /= 2;
}
}
// デキュー処理
internal PQueueJyoutaiDef Dequeue()
{
PQueueJyoutaiDef TopNode = mHeapDict[1];
int LastNode = mHeapDict.Count;
mHeapDict[1] = mHeapDict[LastNode];
mHeapDict.Remove(LastNode);
MinHeapify(1);
return TopNode;
}
// 根ノードを指定し、根から葉へヒープ構築
private void MinHeapify(int pRootNode)
{
if (mHeapDict.Count <= 1) {
return;
}
int Left = pRootNode * 2;
int Right = pRootNode * 2 + 1;
// 左の子、自分、右の子で値が最小のノードを選ぶ
long Smallest = mHeapDict[pRootNode].SumCost;
int SmallestNode = pRootNode;
if (mHeapDict.ContainsKey(Left) && mHeapDict[Left].SumCost < Smallest) {
Smallest = mHeapDict[Left].SumCost;
SmallestNode = Left;
}
if (mHeapDict.ContainsKey(Right) && mHeapDict[Right].SumCost < Smallest) {
Smallest = mHeapDict[Right].SumCost;
SmallestNode = Right;
}
// 子ノードのほうが大きい場合
if (SmallestNode != pRootNode) {
PQueueJyoutaiDef Swap = mHeapDict[SmallestNode];
mHeapDict[SmallestNode] = mHeapDict[pRootNode];
mHeapDict[pRootNode] = Swap;
// 再帰的に呼び出し
MinHeapify(SmallestNode);
}
}
}
#endregion