#467
Unrated
Wormhole Sort
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Farmer John's cows have grown tired of his daily request that they sort themselves before leaving the barn each morning. They have just completed their PhDs in quantum physics, and are ready to speed things up a bit.

This morning, as usual, Farmer John's NN cows (1N1051 \leq N \leq 10^5), conveniently numbered 1N1 \dots N, are scattered throughout the barn at NN distinct locations, also numbered 1N1 \dots N, such that cow ii is at location pip_i. But this morning there are also MM wormholes (1M1051 \leq M \leq 10^5), numbered 1M1 \dots M, where wormhole ii bidirectionally connects location aia_i with location bib_i, and has a width wiw_i (1ai,biN,aibi,1wi1091\le a_i,b_i\le N, a_i\neq b_i, 1\le w_i\le 10^9).

At any point in time, two cows located at opposite ends of a wormhole may choose to simultaneously swap places through the wormhole. The cows must perform such swaps until cow ii is at location ii for 1iN1 \leq i \leq N.

The cows are not eager to get squished by the wormholes. Help them maximize the width of the least wide wormhole which they must use to sort themselves. It is guaranteed that it is possible for the cows to sort themselves.

SCORING:

Test cases 3-5 satisfy N,M1000.N,M\le 1000.Test cases 6-10 satisfy no additional constraints.

입력

The first line contains two integers, NN and MM.

The second line contains the NN integers p1,p2,,pNp_1, p_2, \dots, p_N. It is guaranteed that pp is a permutation of 1N.1\ldots N.

For each ii between 11 and MM, line i+2i+2 contains the integers aia_i, bib_i, and wiw_i.

출력

A single integer: the maximum minimal wormhole width which a cow must squish itself into during the sorting process. If the cows do not need any wormholes to sort themselves, output 1-1.

예제 입력 1

4 4
3 2 1 4
1 2 9
1 3 7
2 3 10
2 4 3

예제 출력 1

9

예제 입력 2

4 1
1 2 3 4
4 2 13

예제 출력 2

-1
코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.