#FT0000063. Số giao cắt lớn nhất của hệ dây dẫn (Maximum Crossings (Hard Version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Số giao cắt lớn nhất của hệ dây dẫn (Maximum Crossings (Hard Version))
Maximum Crossings (Hard Version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are two terminals, each consisting of equal segments ordered from left to right and numbered starting at . One terminal is above the other.
The array requires a straight wire from the interior of upper segment to the interior of lower segment . You may freely choose the endpoints within the segments but cannot place them at a segment boundary.
Two wires cross if they share a point. Place the endpoints to maximize the number of crossing pairs of wires. Equal values of are allowed; wires arriving at the same lower segment may still be made to cross.
Input
The first line contains .
The second line contains integers , the destination lower segments.
Output
Print the maximum achievable number of crossing wire pairs.
Subtasks
-
Subtask 1 (20 points): ; for every element.
-
Subtask 2 (30 points): ; for every element.
-
Subtask 3 (50 points): ; for every element.
Examples
Input
3
2 2 2
Output
3
Explanation
All three wires lead to lower segment . Place their endpoints on that segment in the reverse order of their upper positions. Then wires and , and , and and cross.
There are exactly wire pairs, so no arrangement can have more than crossings. The answer is 3.