#P11258. [GDKOI2023 普及组] 城市建设

[GDKOI2023 普及组] 城市建设

Problem Description

There are NN cities located at nodes 1,2,...,N1, 2, ..., N. As the manager of the cities, Xiaoming wants to connect these NN cities with the minimum total cost. The cost of connecting cities mainly consists of the following two types.

Type 1: Build roads. Building a road between city aa and city bb costs ab|a - b|.

Type 2: Set up management cities. For a city XX, you can upgrade it to a management city at a cost of CC.

Under the constraints that every road must be incident to at least one management city, and every non-management city can connect to only one edge, Xiaoming wants to know the minimum total cost to make all cities connected.

Input Format

The first line contains a positive integer NN, representing the number of cities.

The second line contains an integer CC, representing the cost to set up a management city.

Output Format

One line containing one integer, representing the minimum cost for Xiaoming to connect all cities.

6
3

12
1000
1000
32561
100000
10000
10099799

Hint

Sample Explanation

The minimum cost can be achieved by taking 22 and 55 as management cities, and connecting the five edges (1,2)(1, 2), (2,3)(2, 3), (2,5)(2, 5), (4,5)(4, 5), (5,6)(5, 6).

It can also be achieved by taking 33 or 44 as the management city and connecting all other nodes to it, which also gives the minimum cost.

Constraints

For 20%20\% of the testdata, 1N201 \le N \le 20.

For 40%40\% of the testdata, 1N1031 \le N \le 10^3.

For another 20%20\% of the testdata, 1N1051 \le N \le 10^5, 0C1040 \le C \le 10^4.

For 100%100\% of the testdata, 1N1091 \le N \le 10^9, 0C1090 \le C \le 10^9.

Translated by ChatGPT 5