結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー EmKjpEmKjp
提出日時 2019-12-15 23:32:31
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 644 ms / 2,000 ms
コード長 5,313 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 117,960 KB
実行使用メモリ 39,240 KB
最終ジャッジ日時 2024-07-02 18:30:22
合計ジャッジ時間 21,069 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
18,816 KB
testcase_01 AC 30 ms
19,200 KB
testcase_02 AC 30 ms
19,072 KB
testcase_03 AC 30 ms
18,944 KB
testcase_04 AC 30 ms
19,200 KB
testcase_05 AC 30 ms
19,072 KB
testcase_06 AC 30 ms
18,944 KB
testcase_07 AC 30 ms
18,816 KB
testcase_08 AC 30 ms
19,328 KB
testcase_09 AC 45 ms
23,936 KB
testcase_10 AC 40 ms
21,120 KB
testcase_11 AC 38 ms
21,760 KB
testcase_12 AC 158 ms
36,008 KB
testcase_13 AC 465 ms
37,348 KB
testcase_14 AC 106 ms
36,352 KB
testcase_15 AC 101 ms
37,120 KB
testcase_16 AC 132 ms
36,380 KB
testcase_17 AC 160 ms
39,240 KB
testcase_18 AC 30 ms
18,816 KB
testcase_19 AC 29 ms
19,200 KB
testcase_20 AC 29 ms
18,944 KB
testcase_21 AC 42 ms
21,504 KB
testcase_22 AC 35 ms
19,712 KB
testcase_23 AC 37 ms
20,096 KB
testcase_24 AC 643 ms
36,376 KB
testcase_25 AC 640 ms
36,420 KB
testcase_26 AC 615 ms
36,428 KB
testcase_27 AC 613 ms
36,296 KB
testcase_28 AC 529 ms
36,176 KB
testcase_29 AC 528 ms
36,304 KB
testcase_30 AC 29 ms
19,072 KB
testcase_31 AC 29 ms
19,072 KB
testcase_32 AC 35 ms
19,840 KB
testcase_33 AC 37 ms
20,608 KB
testcase_34 AC 46 ms
22,912 KB
testcase_35 AC 618 ms
36,056 KB
testcase_36 AC 628 ms
36,300 KB
testcase_37 AC 616 ms
36,240 KB
testcase_38 AC 620 ms
36,376 KB
testcase_39 AC 618 ms
36,252 KB
testcase_40 AC 597 ms
36,412 KB
testcase_41 AC 601 ms
36,164 KB
testcase_42 AC 604 ms
36,292 KB
testcase_43 AC 605 ms
36,420 KB
testcase_44 AC 608 ms
36,156 KB
testcase_45 AC 599 ms
36,296 KB
testcase_46 AC 532 ms
36,416 KB
testcase_47 AC 528 ms
36,416 KB
testcase_48 AC 530 ms
36,432 KB
testcase_49 AC 526 ms
36,288 KB
testcase_50 AC 522 ms
36,432 KB
testcase_51 AC 528 ms
36,424 KB
testcase_52 AC 526 ms
36,236 KB
testcase_53 AC 644 ms
36,384 KB
testcase_54 AC 435 ms
36,304 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

using E = System.Linq.Enumerable;

internal partial class Solver {

    public static bool UpdateMax<T>(ref T x, T newValue) where T : IComparable<T> {
        if (x.CompareTo(newValue) < 0) { x = newValue; return true; }
        return false;
    }
    public void Run() {
        var n = ni();
        var k = ni();
        var p = new int[n];
        var d = new int[n];
        for (int i = 0; i < n; i++) {
            p[i] = ni();
            d[i] = ni();
        }

        var order = E.Range(0, n).ToArray();
        Array.Sort(order, (i1, i2) => -p[i1].CompareTo(p[i2]));

        var dp = new int[2, k + 1];
        for (int i = 0; i <= k; i++) {
            dp[0, i] = dp[1, i] = -10000000;
        }
        dp[0, 0] = 0;
        foreach (var i in order) {
            var next = dp.Clone() as int[,];
            for (int j = 0; j <= k; j++) {
                if (j + p[i] <= k) {
                    UpdateMax(ref next[1, j + p[i]], dp[0, j] + d[i]);
                }
                UpdateMax(ref next[0, j], dp[1, j] + d[i]);
            }
            dp = next;
            //dp.Dump();
        }
        cout.WriteLine(dp.Cast<int>().Max());
    }
}


// PREWRITEN CODE BEGINS FROM HERE
internal partial class Solver : Scanner {
    public static void Main(string[] args) {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
    }
    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

internal static class LinqPadExtension {
    public static T Dump<T>(this T obj) {
#if LOCAL
        return LINQPad.Extensions.Dump(obj);
#else
        return obj;
#endif
    }
}

public class Scanner {
    private readonly TextReader Reader;
    private readonly Queue<string> TokenQueue = new Queue<string>();
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return int.Parse(Next(), ci); }
    public long NextLong() { return long.Parse(Next(), ci); }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (TokenQueue.Count == 0) {
            if (!StockTokens()) {
                throw new InvalidOperationException();
            }
        }
        return TokenQueue.Dequeue();
    }

    public bool HasNext() {
        if (TokenQueue.Count > 0) {
            return true;
        }

        return StockTokens();
    }

    private static readonly char[] _separator = new[] { ' ', '\t' };
    private bool StockTokens() {
        while (true) {
            string line = Reader.ReadLine();
            if (line == null) {
                return false;
            }

            string[] tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length == 0) {
                continue;
            }

            foreach (string token in tokens) {
                TokenQueue.Enqueue(token);
            }

            return true;
        }
    }
}
0