結果

問題 No.2617 容量3のナップザック
ユーザー gew1fw
提出日時 2025-06-12 17:01:26
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,246 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 161,152 KB
最終ジャッジ日時 2025-06-12 17:01:40
合計ジャッジ時間 10,144 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 12 WA * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    K = int(input[idx]); idx +=1
    seed = int(input[idx]); idx +=1
    a = int(input[idx]); idx +=1
    b = int(input[idx]); idx +=1
    m = int(input[idx]); idx +=1

    # Generate f sequence
    f = [0] * (2*N + 1)  # 1-based
    f[1] = seed
    for i in range(1, 2*N):
        f[i+1] = (a * f[i] + b) % m

    # Prepare weight and value lists
    w1_list = []
    w2_list = []
    w3_list = []
    for i in range(1, N+1):
        w_i = (f[i] % 3) + 1
        v_i = w_i * f[N + i]
        if w_i == 1:
            w1_list.append(v_i)
        elif w_i == 2:
            w2_list.append(v_i)
        else:
            w3_list.append(v_i)

    # Sort each list in descending order
    w1_list.sort(reverse=True)
    w2_list.sort(reverse=True)
    w3_list.sort(reverse=True)

    # Initialize pointers and total
    w1_idx = 0
    w2_idx = 0
    w3_idx = 0
    total = 0

    for _ in range(K):
        current_max = 0
        best_config = None

        # Check configuration 1: 3 from w1
        if w1_idx + 3 <= len(w1_list):
            sum1 = w1_list[w1_idx] + w1_list[w1_idx+1] + w1_list[w1_idx+2]
            if sum1 > current_max:
                current_max = sum1
                best_config = 'w1_3'

        # Check configuration 2: 1 from w1 and 1 from w2
        if w1_idx < len(w1_list) and w2_idx < len(w2_list):
            sum2 = w1_list[w1_idx] + w2_list[w2_idx]
            if sum2 > current_max:
                current_max = sum2
                best_config = 'w1_w2'

        # Check configuration 3: 1 from w3
        if w3_idx < len(w3_list):
            sum3 = w3_list[w3_idx]
            if sum3 > current_max:
                current_max = sum3
                best_config = 'w3'

        if current_max == 0:
            break  # No more possible configurations

        total += current_max

        # Update indices based on the chosen configuration
        if best_config == 'w1_3':
            w1_idx += 3
        elif best_config == 'w1_w2':
            w1_idx += 1
            w2_idx += 1
        elif best_config == 'w3':
            w3_idx += 1

    print(total)

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