Submission #3724249


Source Code Expand

using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
using System.Text;//テキストの高速出力に必要
class Program
{
	static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');//Splitで区切り文字を指定して複数個受け取る。
		long n = long.Parse(input[0]);
		long w = long.Parse(input[1]);
    long[,] vertexes = new long[n,2];//ここに受け取る
    long[,,] dp = new long[n+1,n+1,3*n+1];//dp[見た数、とった数、超過の重さ] = 価値の最大値
    long answer = 0;
    for(long i = 0; i < n; i++)
    {
      long[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);//1行ごとに受け取る
      vertexes[i,0] = nums[0];
      vertexes[i,1] = nums[1];
    }

    for(int i = 1; i <= n; i++)
    {
      for(int j = 0; j <= n; j++)
      {
        for(int k = 0; k <= 3*n; k++)
        {
          dp[i,j,k] = dp[i-1,j,k];//取らない場合
          if(j-1 >= 0 && k-(vertexes[i-1,0]-vertexes[0,0]) >= 0)
          {
            dp[i,j,k] = 
              Math.Max(dp[i,j,k],dp[i-1,j-1,k-(vertexes[i-1,0]-vertexes[0,0])] + vertexes[i-1,1]);
          }
        }
      }
    }
    
    for(int i = 0; i <= n; i++)
    {
      for(int j = 0; j <= n; j++)
      {
        for(int k = 0; k <= 3*n; k++)
        {
          if(vertexes[0,0]*j + k <= w)
            answer = Math.Max(dp[i,j,k], answer);
        }
      }
    }
    
		Console.WriteLine(answer);
	}
}

Submission Info

Submission Time
Task D - Simple Knapsack
User suikameron
Language C# (Mono 4.6.2.0)
Score 400
Code Size 1528 Byte
Status AC
Exec Time 180 ms
Memory 34656 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 16
Set Name Test Cases
Sample example0, example1, example2, example3
All antigreedy0, antigreedy1, antigreedy2, example0, example1, example2, example3, quarter0, quarter1, quarter2, rand0, rand1, rand2, smallw0, smallw1, smallw2
Case Name Status Exec Time Memory
antigreedy0 AC 179 ms 34656 KB
antigreedy1 AC 179 ms 34656 KB
antigreedy2 AC 180 ms 34656 KB
example0 AC 21 ms 9044 KB
example1 AC 21 ms 11092 KB
example2 AC 21 ms 11092 KB
example3 AC 20 ms 11220 KB
quarter0 AC 167 ms 32608 KB
quarter1 AC 165 ms 32608 KB
quarter2 AC 162 ms 34656 KB
rand0 AC 21 ms 11092 KB
rand1 AC 38 ms 11872 KB
rand2 AC 23 ms 11360 KB
smallw0 AC 158 ms 34656 KB
smallw1 AC 160 ms 32608 KB
smallw2 AC 160 ms 34656 KB