루미 알고리즘

#1633플래티넘Circle Merge

PO 2026 결선코딩출제 Emil Sandberg영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Emil has N bags of stones placed in a circle. Each bag contains some number of stones, where the number of stones in bag i is a_i for 1 ≤ i ≤ N. The bags are arranged in a circle, such that bag 1 is adjacent to bags 2 and N, bag 2 is adjacent to bags 1 and 3, …, and bag N is adjacent to bags N-1 and 1.

Emil wants all bags to contain the same number of stones. In one move, Emil can merge two adjacent bags into a single bag containing the sum of the stones from the two original bags.

What is the minimum number of moves required to make all bags contain the same number of stones?

Input

The first line contains an integer N (1 ≤ N ≤ 2 · 10^5), the number of bags in the circle initially.

The second line contains N integers a_1, a_2, …, a_N (1 ≤ a_i ≤ 5 · 10^12), where a_i is the number of stones in bag i.

Output

Print a single integer, the minimum number of moves required to make all bags contain the same number of stones.

Scoring

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

GroupPointsConstraints
14There exist a solution with the minimum number of moves, where the number of stones in every bag is a_1.
215N ≤ 100
322N ≤ 3000, a_i ≤ 200
429a_i ≤ 200
530No additional constraints.

Explanation of Sample 1

In the first case, it is optimal to merge the bags as follows:

  • Merge the last and the first bag: [1, 2, 3, 4, 5] → [6, 2, 3, 4] (note that the bags are arranged in a circle)
  • Merge the first and second bag: [6, 2, 3, 4] → [8, 3, 4]
  • Merge the second and third bag: [8, 3, 4] → [8, 7]
  • Merge the last two bags: [8, 7] → [15].

In total, 4 moves are required for all bags to contain the same number of stones. It can be shown that it is not possible to do this in fewer moves.

Explanation of Sample 2

In the second case, it is optimal to merge the bags as follows:

  • Merge bags 4 and 5: [5, 2, 3, 1, 4, 2, 1, 1, 1] → [5, 2, 3, 5, 2, 1, 1, 1]
  • Merge bags 6 and 7: [5, 2, 3, 5, 2, 1, 1, 1] → [5, 2, 3, 5, 2, 2, 1]
  • Merge bags 7 and 8: [5, 2, 3, 5, 2, 2, 1] → [5, 2, 3, 5, 2, 3]
  • Merge bags 2 and 3: [5, 2, 3, 5, 2, 3] → [5, 5, 5, 2, 3]
  • Merge bags 4 and 5: [5, 5, 5, 2, 3] → [5, 5, 5, 5].

In total, 5 moves are required for all bags to contain the same number of stones. It can be shown that it is not possible to do this in fewer moves.

예제

예제 1

입력
5
1 2 3 4 5
출력
4

예제 2

입력
9
5 2 3 1 4 2 1 1 1
출력
5

출처 표기

출처: Programmeringsolympiaden 2026년 결선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.