#CC0000016. Chi phí xây dựng mảng có thứ tự (Create Sorted Array through Instructions)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chi phí xây dựng mảng có thứ tự (Create Sorted Array through Instructions)
Create Sorted Array through Instructions
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Start with an empty sorted array and insert one by one. Before inserting , let be the number of existing elements strictly less than and the number strictly greater than . The insertion cost is . Find the total cost modulo . Equal elements count toward neither nor .
Input
The first line contains , the number of insertions. The next line contains integers , to be inserted in order.
Output
Print one integer: the total insertion cost modulo .
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
6
1 2 3 6 5 4
Output
3
Explanation
Inserting costs each because one side is empty. For there are four smaller values and one greater value (), so the cost is . For there are three smaller values () and two greater values (), so the cost is . The total is .