結果

問題 No.1890 Many Sequences Sum Queries
ユーザー yassu0320yassu0320
提出日時 2022-04-05 21:58:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 2,067 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 82,680 KB
最終ジャッジ日時 2024-05-05 10:20:15
合計ジャッジ時間 4,786 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 235 ms
82,512 KB
testcase_01 AC 214 ms
82,504 KB
testcase_02 AC 95 ms
80,064 KB
testcase_03 AC 96 ms
79,884 KB
testcase_04 AC 96 ms
79,744 KB
testcase_05 AC 95 ms
80,096 KB
testcase_06 AC 96 ms
79,996 KB
testcase_07 AC 156 ms
81,700 KB
testcase_08 AC 124 ms
81,128 KB
testcase_09 AC 123 ms
81,436 KB
testcase_10 AC 205 ms
82,680 KB
testcase_11 AC 168 ms
81,816 KB
testcase_12 AC 190 ms
82,324 KB
testcase_13 AC 190 ms
82,160 KB
testcase_14 AC 151 ms
81,552 KB
testcase_15 AC 96 ms
79,892 KB
testcase_16 AC 95 ms
79,760 KB
testcase_17 AC 97 ms
79,668 KB
testcase_18 AC 96 ms
80,092 KB
testcase_19 AC 95 ms
79,896 KB
testcase_20 AC 94 ms
80,028 KB
testcase_21 AC 96 ms
79,640 KB
testcase_22 AC 94 ms
79,740 KB
testcase_23 AC 93 ms
79,612 KB
testcase_24 AC 93 ms
80,020 KB
testcase_25 AC 95 ms
79,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3

from pprint import pprint
from string import ascii_lowercase as letter
from sys import setrecursionlimit, stdin
from typing import Dict, Iterable, Set

try:
    import pypyjit

    pypyjit.set_param("max_unroll_recursion=-1")
except ModuleNotFoundError:
    ...

INF: int = (1 << 62) - 1
MOD1000000007 = 10**9 + 7
MOD998244353 = 998244353
setrecursionlimit(500_000)
readline = stdin.readline
input = lambda: stdin.readline().rstrip("\r\n")


def inputs(type_=int):
    ins = input().split()

    if isinstance(type_, Iterable):
        return [t(x) for t, x in zip(type_, ins)]
    else:
        return list(map(type_, ins))


def input_(type_=int):
    (a, ) = inputs(type_)
    return a


def input1() -> int:
    return int(readline())


inputi = input1


def input2():
    a = readline().split()
    assert len(a) == 2
    a[0] = int(a[0])
    a[1] = int(a[1])
    return a


def input3():
    a = readline().split()
    assert len(a) == 3
    a[0] = int(a[0])
    a[1] = int(a[1])
    a[2] = int(a[2])
    return a


def input4():
    a = readline().split()
    assert len(a) == 4
    a[0] = int(a[0])
    a[1] = int(a[1])
    a[2] = int(a[2])
    a[3] = int(a[3])
    return a


# start coding
def no(x: int) -> int:
    """
    k * (k+1) // 2 >= x を満たす最小のkを返す
    """
    assert x >= 0

    if x == 0:
        return 0

    left = 0
    right = x + 1
    while right - left > 1:
        mid = (left + right) // 2
        if mid * (mid + 1) // 2 < x:
            left = mid
        else:
            right = mid

    return right


n, q = input2()
A = inputs()
sum_ = sum(x * (x + 1) // 2 for x in A)
for _ in range(q):
    s = inputi()
    if s > sum_:
        print(-1)
        continue

    res = 0
    for i in range(n):
        s -= A[i] * (A[i] + 1) // 2
        res += A[i]
        if s == 0:
            print(res)
            break
        elif s < 0:
            res -= A[i]
            s += A[i] * (A[i] + 1) // 2
            # print(108, s)
            res += no(s)
            print(res)
            break
0