#P5037. Stealth Escape

    ID: 18275 Type: Default 1000ms 256MiB

Stealth Escape

Stealth Escape

In the Web Detention Center there are n rooms numbered from 1 to n. Every pair of rooms is connected by a corridor, but each corridor is separated from a room by a door. Since the doors are locked from the outside, opening the door from room i costs ci units of energy. Once a door is opened, it immediately locks again. Traversing any corridor takes exactly 1 second, while opening a door or passing through a room takes no time (but the room is still monitored during that instant).

Each room is under surveillance. Room 1 serves as the control center and is guarded by Guard 1 who prevents anyone (except Yang Shu) from entering. For the remaining rooms, there are guards 2 through n. Guard i monitors every room whose number is a multiple of i (i.e. rooms i, 2i, 3i, etc.). If any guard sees the same intruder in two consecutive time instants—i.e. if you appear in two successive rooms both monitored by that guard—he will report immediately. (Note that each guard only remembers the previous instant and they do not communicate with each other.)

You (Guqiao Wennao) and the police are currently in room x and need to reach room y (the 13th treatment room) without being caught. To ensure you are not detected, your chosen path (which is a sequence of rooms) must never include room 1, and for every two consecutive rooms u and v in the path, they must not be monitored by the same guard. Observing the monitoring rule, this condition is equivalent to requiring that gcd(u, v) = 1 for every consecutive pair (since if u and v share a common divisor greater than 1, then the guard whose number is that divisor would see you twice in a row).

Your task is to find a path from x to y that satisfies the above condition while minimizing the total unlocking cost. The unlocking cost of a path is the sum of the costs for every door you open; that is, if your path is x = r1, r2, …, rk = y, then the total cost is cr1 + cr2 + … + cr(k-1) (note that you do not pay a cost upon entering the final room).

Input Format

The first line contains three integers n, x and y (with n ≥ 2 and x, y ≠ 1), where n is the number of rooms, x is your starting room, and y is the destination room.

The second line contains n integers: c1, c2, …, cn, where ci is the energy cost required to unlock a door leaving room i. (Note that since room 1 is the control center and is off‐limits, its unlocking cost will never be used.)

Output Format

Output a single integer representing the minimum total unlocking cost needed to reach room y from room x without being detected. If it is impossible to reach room y while satisfying the conditions, print -1.

inputFormat

The input consists of two lines.

  1. The first line contains three integers n x y separated by spaces.
  2. The second line contains n integers c1 c2 ... cn separated by spaces.

outputFormat

Output a single integer – the minimum total unlocking cost required to reach room y from room x under the given constraints, or -1 if no such path exists.

sample

5 2 3
1 2 3 4 5
2