QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#3745. Permutation

Statistics

Bobo has a sequence $p_1, p_2, \dots, p_n$ of length $n$ and $m$ pairs $(a_1, b_1), (a_2, b_2), \dots, (a_m, b_m)$. He wants to rearrange the sequence $p$ such that $\sum_{i = 1}^m |p_{a_i} - p_{b_i}|$ is minimized. Find the minimum value.

The input contains multiple test cases. Process until the end of the file.

Each test case starts with a line containing two integers $n$ and $m$.

The second line contains $n$ integers $p_1, p_2, \dots, p_n$.

Each of the next $m$ lines contains two integers $a_i$ and $b_i$.

For each test case, output one integer representing the minimum value.

Constraints

  • $1 \leq n \leq 20$
  • $0 \leq m \leq \frac{n(n - 1)}{2}$
  • $0 \leq p_i \leq 10^9$
  • $1 \leq a_i < b_i \leq n$
  • For $i \neq j$, $(a_i, b_i) \neq (a_j, b_j)$.
  • The number of test cases does not exceed $5,000$, and there is at most $1$ test case with $n > 10$.

Examples

Input 1

3 2
1 2 5
1 2
2 3
3 3
1 2 5
1 2
1 3
2 3
20 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Output 1

4
8
0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.