---
title: "ABC174 E問題の考え方"
date: 2022-05-13
tags: ["AtCoder"]
url: https://aruma256.dev/posts/abc174-e
---

[ABC174 E問題 - Logs](https://atcoder.jp/contests/abc174/tasks/abc174_e) の考え方を書きました。

# 問題を理解する

* 丸太の本数`N`と、丸太の長さリスト `A = [A_1, A_2, ... , A_N]` が与えられる。
* この丸太を`K`回、分割する。
* かしこく分割することで、「どの丸太も○○メートル以内におさめました！」という状態にしたい。
* 最短で何メートル以内を達成することができるか、出力せよ。

## 例

```python
N = 5
A = [1, 3, 2, 1, 2]
K = 4
```

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="281px" height="271px" viewBox="-0.5 -0.5 281 271" style="background-color: rgb(255, 255, 255);"><defs/><g><path d="M 0 166 C 0 162.69 8.95 160 20 160 C 25.3 160 30.39 160.63 34.14 161.76 C 37.89 162.88 40 164.41 40 166 L 40 234 C 40 237.31 31.05 240 20 240 C 8.95 240 0 237.31 0 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 40 166 C 40 169.31 31.05 172 20 172 C 8.95 172 0 169.31 0 166" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 60 6 C 60 2.69 68.95 0 80 0 C 85.3 0 90.39 0.63 94.14 1.76 C 97.89 2.88 100 4.41 100 6 L 100 234 C 100 237.31 91.05 240 80 240 C 68.95 240 60 237.31 60 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 100 6 C 100 9.31 91.05 12 80 12 C 68.95 12 60 9.31 60 6" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 120 86 C 120 82.69 128.95 80 140 80 C 145.3 80 150.39 80.63 154.14 81.76 C 157.89 82.88 160 84.41 160 86 L 160 234 C 160 237.31 151.05 240 140 240 C 128.95 240 120 237.31 120 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 160 86 C 160 89.31 151.05 92 140 92 C 128.95 92 120 89.31 120 86" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 180 166 C 180 162.69 188.95 160 200 160 C 205.3 160 210.39 160.63 214.14 161.76 C 217.89 162.88 220 164.41 220 166 L 220 234 C 220 237.31 211.05 240 200 240 C 188.95 240 180 237.31 180 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 220 166 C 220 169.31 211.05 172 200 172 C 188.95 172 180 169.31 180 166" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 240 86 C 240 82.69 248.95 80 260 80 C 265.3 80 270.39 80.63 274.14 81.76 C 277.89 82.88 280 84.41 280 86 L 280 234 C 280 237.31 271.05 240 260 240 C 248.95 240 240 237.31 240 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 280 86 C 280 89.31 271.05 92 260 92 C 248.95 92 240 89.31 240 86" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><rect x="5" y="240" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 255px; margin-left: 6px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="20" y="260" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="65" y="240" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 255px; margin-left: 66px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">3</div></div></div></foreignObject><text x="80" y="260" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">3</text></switch></g><rect x="125" y="240" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 255px; margin-left: 126px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">2</div></div></div></foreignObject><text x="140" y="260" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">2</text></switch></g><rect x="185" y="240" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 255px; margin-left: 186px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="200" y="260" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="245" y="240" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 255px; margin-left: 246px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">2</div></div></div></foreignObject><text x="260" y="260" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">2</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

このとき、以下のように丸太を `K=4` 回 分割することで、「どの丸太も `1` メートル以内におさめました！」を達成することができる。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="283px" height="241px" viewBox="-0.5 -0.5 283 241" style="background-color: rgb(255, 255, 255);"><defs/><g><path d="M 0 166 C 0 162.69 8.95 160 20 160 C 25.3 160 30.39 160.63 34.14 161.76 C 37.89 162.88 40 164.41 40 166 L 40 234 C 40 237.31 31.05 240 20 240 C 8.95 240 0 237.31 0 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 40 166 C 40 169.31 31.05 172 20 172 C 8.95 172 0 169.31 0 166" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 60 6 C 60 2.69 68.95 0 80 0 C 85.3 0 90.39 0.63 94.14 1.76 C 97.89 2.88 100 4.41 100 6 L 100 234 C 100 237.31 91.05 240 80 240 C 68.95 240 60 237.31 60 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 100 6 C 100 9.31 91.05 12 80 12 C 68.95 12 60 9.31 60 6" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 120 86 C 120 82.69 128.95 80 140 80 C 145.3 80 150.39 80.63 154.14 81.76 C 157.89 82.88 160 84.41 160 86 L 160 234 C 160 237.31 151.05 240 140 240 C 128.95 240 120 237.31 120 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 160 86 C 160 89.31 151.05 92 140 92 C 128.95 92 120 89.31 120 86" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 180 166 C 180 162.69 188.95 160 200 160 C 205.3 160 210.39 160.63 214.14 161.76 C 217.89 162.88 220 164.41 220 166 L 220 234 C 220 237.31 211.05 240 200 240 C 188.95 240 180 237.31 180 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 220 166 C 220 169.31 211.05 172 200 172 C 188.95 172 180 169.31 180 166" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 240 86 C 240 82.69 248.95 80 260 80 C 265.3 80 270.39 80.63 274.14 81.76 C 277.89 82.88 280 84.41 280 86 L 280 234 C 280 237.31 271.05 240 260 240 C 248.95 240 240 237.31 240 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 280 86 C 280 89.31 271.05 92 260 92 C 248.95 92 240 89.31 240 86" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 60 80 L 100 80" fill="none" stroke="#0000ff" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="3 3" pointer-events="stroke"/><path d="M 60 160 L 100 160" fill="none" stroke="#0000ff" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="3 3" pointer-events="stroke"/><path d="M 120 160 L 160 160" fill="none" stroke="#0000ff" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="3 3" pointer-events="stroke"/><path d="M 240 160 L 280 160" fill="none" stroke="#0000ff" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="3 3" pointer-events="stroke"/><rect x="5" y="185" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 200px; margin-left: 6px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="20" y="205" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="65" y="185" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 200px; margin-left: 66px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="80" y="205" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="125" y="185" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 200px; margin-left: 126px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="140" y="205" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="185" y="185" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 200px; margin-left: 186px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="200" y="205" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="245" y="185" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 200px; margin-left: 246px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="260" y="205" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="245" y="105" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 120px; margin-left: 246px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="260" y="125" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="125" y="105" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 120px; margin-left: 126px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="140" y="125" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="65" y="105" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 120px; margin-left: 66px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="80" y="125" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g><rect x="65" y="26" width="30" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 28px; height: 1px; padding-top: 41px; margin-left: 66px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="80" y="46" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">1</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

これより短くすることはできないので、この例では
`1`
と出力すれば正解となる。

## 制約を確認する

|変数名|意味|値の範囲|
|:-:|:-:|:-:|
|N|丸太の本数|1 〜 200000 (2x10<sup>5</sup>)|
|A<sub>i</sub>|丸太の長さ初期値|1 〜 1000000000 (10<sup>9</sup>)|
|K|カット回数|0 〜 1000000000 (10<sup>9</sup>)|

* 入力は全て整数
* 答えは小数点以下を切り上げた値で出力すること
* **2秒以内に答えを出力すること**

# 解法1（単純な方法・TLE）

「現状最も長い丸太を選んで、切る回数を1増やす」をK回繰り返すと、答えにたどり着く。  
プログラム風に記述すると、以下のようになる。（細かい変数は省略）

```ruby
丸太リスト[]

class 丸太 {
    初期長さ = Ai
    切る回数 = 0
    function 現在の長さ() {
        return 初期長さ/切る回数
    }
}

function main() {
    # 入力受け取り
    丸太リストを作成
    # シミュレーション
    K回繰り返す {
        対象の丸太 = 最も長い丸太を取得()
        対象の丸太.切る回数 += 1
    }
    # 解答
    最長の丸太 = 最も長い丸太を取得()
    print(最長の丸太.現在の長さ())
}

function 最も長い丸太を取得() {
    ID = 1 〜 N について {
        丸太リスト[ID].現在の長さ()
    }
    return 最も長かった丸太
}
```

しかし、この実装ではシミュレーション部分の**ループ回数が膨大になってしまう。**

```ruby
K回繰り返す { # 10^9 ループ
    ID = 1 〜 N について { # 2*10^5 ループ
    }
}
```

この二重ループでは、必ず `K * N` 回のループ処理が実行される。  
最悪ケース（つまり、N, K どちらも最大値）では  
`2*10^5 * 10^9 = 2*10^14 = 200000000000000` 回のループ処理が発生する。

仮に、1GHzのCPUが1クロックにつき1回ループ処理を行えたとしても、
200000秒 ... **2日以上かかってしまう。**

「動作の早いC言語なら...」というレベルではないので、
**2秒以内** という制約に間に合うよう、アルゴリズムを改善する。

# 解法2（少し工夫・TLE）

発想を転換する。

考え方

1. 答えを一発で求めるのは難しそうだ。
1. 答えを**探索によって**求められないか？
1. 答えは、必ず 1 〜 `max(A)` の範囲内にある
    * 何回分割しても、0にはならない（小数点以下切り上げ）
    * `max(A)` = 初期状態の最大長 は、分割前から達成しているため、確実に達成可能

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="462px" height="271px" viewBox="-0.5 -0.5 462 271" style="background-color: rgb(255, 255, 255);"><defs/><g><path d="M 1 141 C 1 137.69 9.95 135 21 135 C 26.3 135 31.39 135.63 35.14 136.76 C 38.89 137.88 41 139.41 41 141 L 41 249 C 41 252.31 32.05 255 21 255 C 9.95 255 1 252.31 1 249 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 41 141 C 41 144.31 32.05 147 21 147 C 9.95 147 1 144.31 1 141" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 61 21 C 61 17.69 69.95 15 81 15 C 86.3 15 91.39 15.63 95.14 16.76 C 98.89 17.88 101 19.41 101 21 L 101 249 C 101 252.31 92.05 255 81 255 C 69.95 255 61 252.31 61 249 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 101 21 C 101 24.31 92.05 27 81 27 C 69.95 27 61 24.31 61 21" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 121 221 C 121 217.69 129.95 215 141 215 C 146.3 215 151.39 215.63 155.14 216.76 C 158.89 217.88 161 219.41 161 221 L 161 249 C 161 252.31 152.05 255 141 255 C 129.95 255 121 252.31 121 249 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 161 221 C 161 224.31 152.05 227 141 227 C 129.95 227 121 224.31 121 221" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 181 111 C 181 107.69 189.95 105 201 105 C 206.3 105 211.39 105.63 215.14 106.76 C 218.89 107.88 221 109.41 221 111 L 221 249 C 221 252.31 212.05 255 201 255 C 189.95 255 181 252.31 181 249 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 221 111 C 221 114.31 212.05 117 201 117 C 189.95 117 181 114.31 181 111" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 1 15 L 281 15" fill="none" stroke="#0000ff" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="9 9" pointer-events="stroke"/><path d="M 1 254 L 281 255" fill="none" stroke="#ff0000" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="9 9" pointer-events="stroke"/><rect x="281" y="0" width="139" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 137px; height: 1px; padding-top: 15px; margin-left: 283px;"><div data-drawio-colors="color: #0000FF; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 255); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">確実に可能</div></div></div></foreignObject><text x="283" y="20" fill="#0000FF" font-family="Helvetica" font-size="18px">確実に可能</text></switch></g><rect x="281" y="240" width="139" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 137px; height: 1px; padding-top: 255px; margin-left: 283px;"><div data-drawio-colors="color: #FF0000; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(255, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">確実に不可能</div></div></div></foreignObject><text x="283" y="260" fill="#FF0000" font-family="Helvetica" font-size="18px">確実に不可能</text></switch></g><path d="M 269.5 15 L 262 15 Q 254.5 15 254.5 25 L 254.5 125 Q 254.5 135 247 135 L 243.25 135 Q 239.5 135 247 135 L 250.75 135 Q 254.5 135 254.5 145 L 254.5 245 Q 254.5 255 262 255 L 269.5 255" fill="none" stroke="rgb(0, 0, 0)" stroke-width="3" stroke-miterlimit="10" transform="translate(254.5,0)scale(-1,1)translate(-254.5,0)" pointer-events="all"/><rect x="281" y="120" width="180" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 178px; height: 1px; padding-top: 135px; margin-left: 283px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">答えはこの中にある</div></div></div></foreignObject><text x="283" y="140" fill="#000000" font-family="Helvetica" font-size="18px">答えはこの中にある</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

「最短で何メートル以内を達成することができるか」ではなく
「Cメートル以内を達成することができるか」ならば、N回以内のループで判定できる。
（Cメートル以内になるように、一括で分割していくことができるため）

そこで、

* 1メートル以内は可能か
* 無理なら、2メートル以内は可能か
* 無理なら、3メートル以内は可能か
* ...

短い方から順に試していき、最初に「可能」と判定されたラインが答えとなる。

解法1と同様に、最悪ケースのループ回数を見積もってみる。  
工夫により、答えが `1` となるとき（図の一番下のラインのとき）は、ループ回数 `N = 200000` 回で答えを出力できるようになった。  
しかし、答えが `max(A)` のとき（図の一番上のラインのとき）は、ループ回数が `N * max(A) = 200000000000000` 回 と、まだ多すぎる。

# 解法3（AC）

解法2では、初めて「可能」となるラインを特定するために、max(A)回のループ処理を行なっていた。ここを高速化したい。

「ある数直線の範囲内に存在する境界線を特定する」という処理は、 **二分探索** というアルゴリズムによって高速に実現できる。

まずは、ちょうど中間地点について実現可能かを判定してみる。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="498px" height="256px" viewBox="-0.5 -0.5 498 256" style="background-color: rgb(255, 255, 255);"><defs/><g><path d="M 1 126 C 1 122.69 9.95 120 21 120 C 26.3 120 31.39 120.63 35.14 121.76 C 38.89 122.88 41 124.41 41 126 L 41 234 C 41 237.31 32.05 240 21 240 C 9.95 240 1 237.31 1 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 41 126 C 41 129.31 32.05 132 21 132 C 9.95 132 1 129.31 1 126" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 61 6 C 61 2.69 69.95 0 81 0 C 86.3 0 91.39 0.63 95.14 1.76 C 98.89 2.88 101 4.41 101 6 L 101 234 C 101 237.31 92.05 240 81 240 C 69.95 240 61 237.31 61 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 101 6 C 101 9.31 92.05 12 81 12 C 69.95 12 61 9.31 61 6" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 121 206 C 121 202.69 129.95 200 141 200 C 146.3 200 151.39 200.63 155.14 201.76 C 158.89 202.88 161 204.41 161 206 L 161 234 C 161 237.31 152.05 240 141 240 C 129.95 240 121 237.31 121 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 161 206 C 161 209.31 152.05 212 141 212 C 129.95 212 121 209.31 121 206" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 181 96 C 181 92.69 189.95 90 201 90 C 206.3 90 211.39 90.63 215.14 91.76 C 218.89 92.88 221 94.41 221 96 L 221 234 C 221 237.31 212.05 240 201 240 C 189.95 240 181 237.31 181 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 221 96 C 221 99.31 212.05 102 201 102 C 189.95 102 181 99.31 181 96" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 1 120 L 281 120" fill="none" stroke="#0000ff" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="9 9" pointer-events="stroke"/><path d="M 1 239 L 281 240" fill="none" stroke="#ff0000" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="9 9" pointer-events="stroke"/><rect x="281" y="105" width="50" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 48px; height: 1px; padding-top: 120px; margin-left: 283px;"><div data-drawio-colors="color: #0000FF; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 255); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">可能</div></div></div></foreignObject><text x="283" y="125" fill="#0000FF" font-family="Helvetica" font-size="18px">可能</text></switch></g><rect x="281" y="225" width="139" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 137px; height: 1px; padding-top: 240px; margin-left: 283px;"><div data-drawio-colors="color: #FF0000; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(255, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">確実に不可能</div></div></div></foreignObject><text x="283" y="245" fill="#FF0000" font-family="Helvetica" font-size="18px">確実に不可能</text></switch></g><path d="M 269.5 120 L 262 120 Q 254.5 120 254.5 130 L 254.5 170 Q 254.5 180 247 180 L 243.25 180 Q 239.5 180 247 180 L 250.75 180 Q 254.5 180 254.5 190 L 254.5 230 Q 254.5 240 262 240 L 269.5 240" fill="none" stroke="rgb(0, 0, 0)" stroke-width="3" stroke-miterlimit="10" transform="translate(254.5,0)scale(-1,1)translate(-254.5,0)" pointer-events="all"/><rect x="281" y="165" width="180" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 178px; height: 1px; padding-top: 180px; margin-left: 283px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">答えはこの中にある</div></div></div></foreignObject><text x="283" y="185" fill="#000000" font-family="Helvetica" font-size="18px">答えはこの中にある</text></switch></g><rect x="311" y="105" width="185.5" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 184px; height: 1px; padding-top: 120px; margin-left: 313px;"><div data-drawio-colors="color: #0000FF; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 14px; font-family: Helvetica; color: rgb(0, 0, 255); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">（シミュレーションで確認）</div></div></div></foreignObject><text x="313" y="124" fill="#0000FF" font-family="Helvetica" font-size="14px">（シミュレーションで確認）</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

答えが存在する範囲のちょうど中間地点について達成可能かを調べ、範囲を狭めていくことを繰り返す。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="518px" height="241px" viewBox="-0.5 -0.5 518 241" style="background-color: rgb(255, 255, 255);"><defs/><g><path d="M 1 126 C 1 122.69 9.95 120 21 120 C 26.3 120 31.39 120.63 35.14 121.76 C 38.89 122.88 41 124.41 41 126 L 41 234 C 41 237.31 32.05 240 21 240 C 9.95 240 1 237.31 1 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 41 126 C 41 129.31 32.05 132 21 132 C 9.95 132 1 129.31 1 126" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 61 6 C 61 2.69 69.95 0 81 0 C 86.3 0 91.39 0.63 95.14 1.76 C 98.89 2.88 101 4.41 101 6 L 101 234 C 101 237.31 92.05 240 81 240 C 69.95 240 61 237.31 61 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 101 6 C 101 9.31 92.05 12 81 12 C 69.95 12 61 9.31 61 6" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 121 206 C 121 202.69 129.95 200 141 200 C 146.3 200 151.39 200.63 155.14 201.76 C 158.89 202.88 161 204.41 161 206 L 161 234 C 161 237.31 152.05 240 141 240 C 129.95 240 121 237.31 121 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 161 206 C 161 209.31 152.05 212 141 212 C 129.95 212 121 209.31 121 206" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 181 96 C 181 92.69 189.95 90 201 90 C 206.3 90 211.39 90.63 215.14 91.76 C 218.89 92.88 221 94.41 221 96 L 221 234 C 221 237.31 212.05 240 201 240 C 189.95 240 181 237.31 181 234 Z" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 221 96 C 221 99.31 212.05 102 201 102 C 189.95 102 181 99.31 181 96" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 1 120 L 281 120" fill="none" stroke="#0000ff" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="9 9" pointer-events="stroke"/><path d="M 1 179 L 281 180" fill="none" stroke="#ff0000" stroke-width="3" stroke-miterlimit="10" stroke-dasharray="9 9" pointer-events="stroke"/><rect x="281" y="105" width="50" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 48px; height: 1px; padding-top: 120px; margin-left: 283px;"><div data-drawio-colors="color: #0000FF; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 255); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">可能</div></div></div></foreignObject><text x="283" y="125" fill="#0000FF" font-family="Helvetica" font-size="18px">可能</text></switch></g><rect x="281" y="165" width="60" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 58px; height: 1px; padding-top: 180px; margin-left: 283px;"><div data-drawio-colors="color: #FF0000; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(255, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">不可能</div></div></div></foreignObject><text x="283" y="185" fill="#FF0000" font-family="Helvetica" font-size="18px">不可能</text></switch></g><path d="M 269.5 120 L 262 120 Q 254.5 120 254.5 130 L 254.5 140 Q 254.5 150 247 150 L 243.25 150 Q 239.5 150 247 150 L 250.75 150 Q 254.5 150 254.5 160 L 254.5 170 Q 254.5 180 262 180 L 269.5 180" fill="none" stroke="rgb(0, 0, 0)" stroke-width="3" stroke-miterlimit="10" transform="translate(254.5,0)scale(-1,1)translate(-254.5,0)" pointer-events="all"/><rect x="281" y="135" width="180" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 178px; height: 1px; padding-top: 150px; margin-left: 283px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">答えはこの中にある</div></div></div></foreignObject><text x="283" y="155" fill="#000000" font-family="Helvetica" font-size="18px">答えはこの中にある</text></switch></g><rect x="311" y="105" width="185.5" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 184px; height: 1px; padding-top: 120px; margin-left: 313px;"><div data-drawio-colors="color: #0000FF; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 14px; font-family: Helvetica; color: rgb(0, 0, 255); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">（シミュレーションで確認）</div></div></div></foreignObject><text x="313" y="124" fill="#0000FF" font-family="Helvetica" font-size="14px">（シミュレーションで確認）</text></switch></g><rect x="331" y="165" width="185.5" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 184px; height: 1px; padding-top: 180px; margin-left: 333px;"><div data-drawio-colors="color: #FF0000; " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 14px; font-family: Helvetica; color: rgb(255, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">（シミュレーションで確認）</div></div></div></foreignObject><text x="333" y="184" fill="#FF0000" font-family="Helvetica" font-size="14px">（シミュレーションで確認）</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

可能と不可能の境界を特定できたら、最終的な「可能」のラインが答えとなる。  
ループ回数は `N * log2(max(A))` で、6000000回 程度となる。

# 解法の比較

||計算量|最大ループ回数のイメージ|
|-|-|-:|
|解法1|`O(K * N)`|200000000000000 くらい|
|解法2|`O(N * max(A))`|200000000000000 くらい|
|解法3|`O(N * log(max(A)))`|6000000 くらい|

# 回答例

* [Ruby (2.7.1)](https://atcoder.jp/contests/abc174/submissions/31415064)
    * 実行時間 : 397 ms
* [Python (3.8.2)](https://atcoder.jp/contests/abc174/submissions/31414398)
    * 実行時間 : 502 ms
