아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Controlled Inflation

시간 제한5초메모리 제한1024 MB

요약
각 고객의 물건 순서는 자유롭게 정하되 고객 순서는 지키면서, 0에서 시작해 모든 목표 압력을 맞추는 최소 버튼 횟수를 구한다.
난이도

보통10점 중 6점

유형
동적 계획법, 그리디
정답자
아직 제출이 없습니다

문제

The lines at the air pump at your gas station are getting too long! You want to optimize the process to help customers more quickly inflate their tires, sports balls, giant parade balloon animals, and other products.

The pump is automatic: you set the pressure to a specific number of pascals and plug the pump into the inflatable product, and it will inflate as needed to that exact pressure. There are only two buttons on the pump: up and down. They increase and decrease the target pressure, respectively, by exactly 11 pascal.

There is a line of NN customers, each of whom brings exactly PP products that they need to get inflated by the pump. You know the target pressure of each product. You can inflate the products from a customer in any order you want, but you cannot change the order of the customers. Specifically, you must inflate all products from the ii⁠-th customer before inflating any from the (i+1)(i+1)⁠-th customer. In between handling two products, if those two products have different target pressures, you need to use the buttons on the pump.

The pump is initially set to 00 pascals, and it can be left at any number after all products of all customers have been inflated. If you order the products of each customer optimally, what is the minimum number of button presses you need?

입력

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing two integers, NN and PP: the number of customers and the number of products each customer brings, respectively. Then, NN lines follow. The ii-th of these lines contains PP integers X_i,1,X_i,2,…,X_i,PX\_{i,1},X\_{i,2},\dots,X\_{i,P}, representing that the jj-th product that the ii-th customer brings has a target pressure of X_i,jX\_{i,j} pascals.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum number of button presses needed to inflate all products according to their specified pressures.

제한

  • 1≤T≤1001≤T≤100.
  • 1≤X_i,j≤1091≤X\_{i,j}≤10^9, for all i,ji,j.

힌트

In Sample Case #1, an optimal way to use the pump is:

  1. press up 1010 times, setting the pump to 1010; pump the product (from customer 1) that needs 1010 pascals,
  2. press up 3030 times, setting the pump to 4040; pump the product (from customer 1) that needs 4040 pascals,
  3. press down 1010 times, setting the pump to 3030; pump the product (from customer 1) that needs 3030 pascals,
  4. press down 1010 times, setting the pump to 2020; pump the product (from customer 2) that needs 2020 pascals,
  5. press up 3030 times, setting the pump to 5050; pump the product (from customer 2) that needs 5050 pascals,
  6. press up 1010 times, setting the pump to 6060; pump the product (from customer 2) and the two products (from customer 3) that need 6060 pascals, and finally
  7. press down 1010 times, setting the pump to 5050; pump the product (from customer 3) that needs 5050 pascals.

This is a total of 110110 button presses.

In Sample Case #2, notice that the answer can be larger than 232232.

예제1

  1. 예제 1

    입력
    2
    3 3
    30 10 40
    20 50 60
    60 60 50
    5 2
    1 1000000000
    500000000 1000000000
    1 1000000000
    500000000 1
    1 1000000000
    
    예상 출력
    Case #1: 110
    Case #2: 4999999996