AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第16回PAST H 休暇


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5 2");
            WillReturn.Add("3 2 4 1 5");
            //12
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 2");
            WillReturn.Add("3 5 4 1 2");
            //11
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 0");
            WillReturn.Add("1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000");
            //10000000000
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long M = wkArr[1];

        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        // 失点合計[宿題した日数 ,  前日に宿題をしたか?]なDP表
        long?[,] PrevDP = new long?[M + 1, 2];
        PrevDP[0, 0] = 0;

        foreach (long EachA in AArr) {
            long?[,] CurrDP = new long?[M + 1, 2];

            Action<long, long, long> UpdateAct = (pNewI, pNewJ, pNewVal) =>
            {
                if (CurrDP[pNewI, pNewJ].HasValue) {
                    if (CurrDP[pNewI, pNewJ].Value <= pNewVal) {
                        return;
                    }
                }
                CurrDP[pNewI, pNewJ] = pNewVal;
            };

            for (long I = 0; I <= M; I++) {
                for (long J = 0; J <= 1; J++) {
                    if (PrevDP[I, J].HasValue == false) continue;
                    if (J == 0) {
                        long NewI = I + 1;
                        if (NewI <= M) {
                            UpdateAct(NewI, 1, PrevDP[I, J].Value + EachA);
                        }
                    }
                    UpdateAct(I, 0, PrevDP[I, J].Value);
                }
            }
            PrevDP = CurrDP;
        }

        long SumVal = AArr.Sum();

        var AnswerList = new List<long>();
        for (long J = 0; J <= 1; J++) {
            if (PrevDP[M, J].HasValue) {
                AnswerList.Add(SumVal - PrevDP[M, J].Value);
            }
        }
        Console.WriteLine(AnswerList.Max());
    }
}


解説

失点合計[宿題した日数 ,  前日に宿題をしたか?]なDP表
を更新してます。