#P16798. [蓝桥杯 2026 国 B] Token 词元

[蓝桥杯 2026 国 B] Token 词元

Problem Description

In March 2026, the China National Committee for Terminology in Science and Technology announced that it would prioritize setting the Chinese translation of the AI term “Token” as “词元”, and put it on trial for the whole society. Since then, the platform where Xiao Lan works has started using “词元” as the official unit for monthly statistics.

There are nn APIs on the platform, numbered from 11 to nn from left to right. The number of tokens consumed this month by API ii is pip_i.

At the end of the month, the platform needs to assign an inspection level for each API for the next month based on token consumption. Each API will be assigned a positive integer inspection level. A higher inspection level means the API needs closer monitoring. However, the platform does not assign inspection levels directly by the absolute value of token consumption, but instead refers to the relative relationships between adjacent APIs.

For API ii, define cic_i as the number of adjacent APIs whose token consumption is less than pip_i. Here, adjacent APIs are those whose indices differ by 11. That is, API ii can only be adjacent to API i−1i-1 and API i+1i+1. API 11 only has a right neighbor, and API nn only has a left neighbor; all other APIs have both neighbors. Therefore, ci∈{0,1,2}c_i \in \{0, 1, 2\}.

Let the inspection level of API ii be aia_i. The platform requires that for any adjacent APIs ii and i+1i+1:

  • If ci<ci+1c_i < c_{i+1}, then ai<ai+1a_i < a_{i+1}.
  • If ci>ci+1c_i > c_{i+1}, then ai>ai+1a_i > a_{i+1}.
  • If ci=ci+1c_i = c_{i+1}, there is no requirement on the relation between aia_i and ai+1a_{i+1}.

There may be multiple inspection level assignments that satisfy the rules. Because inspection resources are limited, the platform wants the sum of inspection levels to be as small as possible (i.e., minimize ∑i=1nai\sum_{i=1}^{n} a_i).

Now, please help the platform find this minimum value.

Input Format

The input has two lines.

The first line contains a positive integer nn, representing the number of APIs.

The second line contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n, where pip_i represents the number of tokens consumed this month by API ii.

Output Format

Output one line containing a positive integer, representing the minimum possible sum of inspection levels over all APIs, under the condition that all adjacent inspection level requirements are satisfied.

3
1 2 3
4

Hint

Constraints

For 30%30\% of the testdata, 2≤n≤2×1032 \le n \le 2 \times 10^3, 1≤pi≤1051 \le p_i \le 10^5.

For all testdata, 2≤n≤2×1052 \le n \le 2 \times 10^5, 1≤pi≤1091 \le p_i \le 10^9.

Translated by ChatGPT 5