P18125 [ICPC 2026 Seoul R] Glowing Devices
Description
A lab has $n$ self-luminous devices, numbered from $1$ to $n$. Each device is either **glowing** or **dark**. Once a device starts glowing, it never goes dark again.
A dark device does not need an external power source to start glowing. It is triggered by the light of the other devices. Device $i$ has a **threshold** $t_i$ ($1 \le t_i \le n-1$). As soon as at least $t_i$ devices are glowing, device $i$ starts glowing too.
The devices are initially dark, and none of them can start the (glowing) chain on its own. To start the chain, you pick a set $S$ of devices and **force** each of them to glow. Forcing device $i$ costs $w_i$. The devices in $S$ start glowing all at once, and every other device starts out dark. After that, the following rule is applied over and over:
> Let $L$ be the number of devices that are currently glowing (including the devices in $S$). If some dark device $i$ satisfies $L \ge t_i$, device $i$ starts glowing.
The process stops when no dark device satisfies this condition. A glowing device never goes dark, so $L$ never decreases. Therefore, the set of devices glowing at the end depends only on $S$, not on the order in which devices start glowing.
A set $S$ is **valid** if all $n$ devices are glowing when the process stops. The cost of $S$ is $\sum_{i\in S} w_i$.
Given thresholds and costs of devices, find the minimum cost of a valid set $S$.
Forcing every device, $S=\{1,2,\ldots,n\}$, is always valid, so an answer always exists. Since every threshold is at least $1$, no device starts glowing unless at least one device is forced, so $S$ must be non-empty.
Input Format
Your program is to read from standard input. The input starts with a line containing an integer $n$ ($2 \le n \le 100{,}000$), where $n$ is the number of devices. The devices are numbered from $1$ to $n$.
The second line contains $n$ integers $t_1,t_2,\ldots,t_n$ ($1 \le t_i \le n-1$), the thresholds of the devices.
The third line contains $n$ integers $w_1,w_2,\ldots,w_n$ ($1 \le w_i \le 10^9$), the costs of forcing each device to glow.
Output Format
Your program is to write to standard output. Print exactly one line. The line should contain one integer that is the minimum total cost of the devices you force to glow so that every device ends up glowing.