PivotOJ

Abridged Reading

시간 제한: 4000ms메모리 제한: 1024MB출처: ICPC ECNA 2021-2022BOJ 24556

문제

Miss Othmar is a grade school teacher who uses a very interesting textbook in her science class.  All of the chapters in the book have material that depends on at most one previous chapter's material, while several chapters are labeled "Culminating Concept" chapters that have no chapters depending on them –-- they basically represent the culmination of the stream of material in all the previous chapters that must be read before them.  Chapters that are not Culminating Concept chapters are referred to as "prerequisite chapters."

Because of various delays caused by the pandemic, Miss Othmar is far behind where she wants to be in the class.  It's too late to try to cover all of the Culminating Concept chapters in the book (and their required prerequisite chapters) so she has decided to cover just two more Culminating Concept chapters.  To give the kids a break she has decided to pick the two Culminating Concept chapters which require the least amount of reading –-- this would include not only those chapters but their prerequisite chapters as well.

입력

Input starts with a line containing two integers nn mm where nn (2n10002 \leq n \leq 1\,000) indicates the number of chapters (numbered 11 to nn) and mm (0m<n0 \leq m < n) indicates the number of chapter dependencies.  After this are nn positive values indicating the number of pages in each chapter. These values will be on one or more lines and the number of pages in any chapter is 1000 \leq 1\,000.  After this are mm lines each containing two  integers aa bb (1a<bn)(1 \leq a < b \leq n) indicating that chapter aa must be read before chapter bb.  No chapter appears as the second integer in these lines more than once.  There will be at least two Culminating Concept chapters.

출력

Output the minimum number of pages that need to be read in order to complete two Culminating Concept chapters.

예제

예제 1

입력
7 6
10 9 6 4 2 10 12
1 2
1 3
2 4
2 5
3 6
3 7
출력
25

예제 2

입력
4 2
10 7 4 6
1 4
2 3
출력
27
코드를 제출하려면 로그인하세요.