結果

問題 No.1211 円環はお断り
ユーザー qwewe
提出日時 2025-04-24 12:26:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,078 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 82,236 KB
実行使用メモリ 109,276 KB
最終ジャッジ日時 2025-04-24 12:27:25
合計ジャッジ時間 5,633 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    K = int(input[idx])
    idx += 1
    A = list(map(int, input[idx:idx+N]))
    
    total_sum = sum(A)
    if K == 0:
        print(0)
        return
    
    def is_possible(M):
        if M == 0:
            return True
        total = sum(A)
        if total < K * M:
            return False
        
        # Check linear case
        cnt = 0
        current_sum = 0
        for num in A:
            current_sum += num
            if current_sum >= M:
                cnt += 1
                current_sum = 0
                if cnt >= K:
                    return True
        if cnt >= K:
            return True
        
        if K == 1:
            return False
        
        # Precompute prefix sums
        pre = [0] * N
        pre[0] = A[0]
        for i in range(1, N):
            pre[i] = pre[i-1] + A[i]
        
        # Precompute max_segments for the remaining part (i+1 to N-2)
        max_segments = [0] * N
        current_sum = 0
        cnt_seg = 0
        for i in range(N-2, -1, -1):
            current_sum += A[i]
            if current_sum >= M:
                cnt_seg += 1
                current_sum = 0
            max_segments[i] = cnt_seg
        
        # Check for possible i in 0..N-3
        for i in range(N-2 + 1):  # i can be up to N-2, but need i <= N-3
            if i > N-3:
                continue
            if pre[i] + A[-1] < M:
                continue
            next_i = i + 1
            if next_i > N-2:
                if K-1 == 0:
                    return True
                else:
                    continue
            if max_segments[next_i] >= K-1:
                return True
        return False
    
    low = 0
    high = total_sum
    answer = 0
    while low <= high:
        mid = (low + high) // 2
        if is_possible(mid):
            answer = mid
            low = mid + 1
        else:
            high = mid - 1
    print(answer)

if __name__ == '__main__':
    main()
0