Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Starter Round 8Rated: < 1700
  3. 23B
Sponsor
Abhishek Choudhary

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

23B. Stay for a Song

1000 pts·1s·256 MB

Constraints

Input

Output

Samples

Input
4
4 2 7 3
Explanation

Listening for 9 seconds is insufficient: if you join half a second after the third song starts, you need 6.5 more seconds for it to finish and then 3 seconds to hear the fourth song completely. This takes 9.5 seconds in total.

Listening for 10 seconds always allows you to hear a complete song, regardless of when you join.

Output
10
Input
1
6
Explanation

The same 6-second song repeats forever. If you join half a second after a playback starts, you must wait 5.5 seconds for that playback to finish and then listen to the next playback for 6 seconds. Thus, 11 seconds are insufficient, while 12 seconds always suffice.

Output
12
Input
4
8 1 2 7
Explanation

The first song starts again immediately after the fourth song ends. If you join half a second after the fourth song starts, you need 6.5 more seconds for it to finish and then 8 seconds to hear the first song completely. This takes 14.5 seconds, so 14 seconds are insufficient.

Listening for 15 seconds always allows you to hear a complete song.

Output
15

A radio station plays nnn songs in a fixed order, repeating the playlist forever. The song durations are a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​ seconds. There are no gaps between songs, including between song nnn and song 111.

You turn on the radio at an arbitrary moment, possibly in the middle of a song. This moment is not necessarily an integer number of seconds.

Find the smallest integer TTT such that listening continuously for TTT seconds guarantees that you hear at least one complete song, regardless of when you turn on the radio.

A song counts only if you hear one playback of it from its beginning to its end. A playback starting exactly when you turn on the radio or ending exactly when you stop listening also counts.

  • 1≤n≤2×1051 \le n \le 2 \times 10^51≤n≤2×105
  • 1≤ai≤1091 \le a_i \le 10^91≤ai​≤109
nnn a1a2…ana_1 \quad a_2 \quad \ldots \quad a_na1​a2​…an​

The first line contains the number of songs nnn.

The second line contains their durations in playback order.

TTT

Print the smallest integer listening duration that guarantees hearing a complete song.