QCC P6 - Freedom!

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

Finally, COVID-19 has been eradicated! To celebrate, you and your neighbours decide to meet up to have a massive party. You and your neighbours live along a street with N houses numbered 1 to N. Each house is at a height of ai. The travel cost from house x to house y is defined as: k=min(x,y)max(x,y)1|ak+1ak| or 0 if x=y.

To have a party at house x, everyone must come to house x. The total cost of having the party at house x is the sum of the travel costs of each individual house travelling to house x. However, not everyone on your street is willing to host such a massive party. More specifically, out of you and your neighbours, only Q people are willing to host it. To help determine the best house to have the party, you have compiled all the house numbers into a list h that would be willing to host a party. For each house in h, print out the cost of having a party at hi.

Input Specification

The first line will contain the positive integer N, the number of houses on your street.

The second line will contain N space-separated positive integers, ai, the height of the ith house.

The third line will contain the positive integer Q, the number of people willing to host the party.

The next Q lines will contain a positive integer hi.

Output Specification

For each house number hi, print out the corresponding cost of hosting a party at hi.

Constraints

For all subtasks:

3N106

0ai106

1Q106

1hiN

Subtask 1 [10%]

3N400

1Q400

Subtask 2 [20%]

3N4×103

1Q4×103

Subtask 3 [70%]

No additional constraints.

Sample Input 1

Copy
4
1 2 5 1
2
2
4

Sample Output 1

Copy
11
19

Sample Input 2

Copy
8
1 3 2 5 999 0 6 6
5
6
5
4
3
2

Sample Output 2

Copy
8996
6998
6998
7004
7008

Comments

There are no comments at the moment.