PNTNG - Editorial



Author: Konstantin Sokol
Tester: Tasnim Imran Sunny
Editorialist: Praveen Dhinwa




sorting, greedy


Given a three dimensional table of dimensions N * M * H. N * M is the dimension of the base of the table. H is the number of layers
of the table. You can paint at most T k cells in k th layer with cost of painting a cell in the level being
C k .

Find out the minimum cost needed to paint such that there is no vertical column which is unpainted on every layer.


  • You can select exactly N * M cells to paint such that the condition of no vertical column being unpainted is satisfied.

  • Use greedy algorithm for painting the cells. Paint the least cost N * M cells.

  • If there are not enough cells available for painting, then answer will be impossible.


As said earlier, as cost of each painting each cell is positive, we will not select more than N * M cells.
So we will select N * M cells having least painting cost.

It can be done by using a simple greedy algorithm. We will sort the layers in increasing order of cost and will take the least costly N * M element.

If there are not enough cells available for painting, then the answer will be impossible. In other words, we can say that if the number of cells
available for painting are less than N * M, then the answer is impossible.

Number of cells which can be painted will be T 1 + T 2 + … T k .

Pseudo Code:

	Sort the layers in increasing order of C_k.
	toPaint = N * M;
	// toPaint number of cells to be painted.
	ans = 0
	// ans denotes the cost of the operations.
	for i = 1 to H:
		canPaint = min(toPaint, T_k);
		ans += canPaint * C_k
		toPaint -= canPaint
	if (toPaint > 0):
		// it means that you can not paint N * M cells, the answer will be impossible.
		print "impossible".
		print ans;

O(H log H) : We need sorting + another O(H) loop. So time complexity will be O(H log H).

Possible reasons of getting wrong answers
You should make n and m long long rather than int, because n = 10^12 and m = 1 and vice versa.


Author’s solution
Tester’s solution

1 Like

I used quicksort , minheap, Python min function everything then also TLE. Feeling irritated. plzz give test cases where my code fails??

I did the same thing! WHY did I get WA?? Please help!

EDIT: I figured out my problem. I was finding the maximum cost instead of minimum. -_- I think I will go kill myself now…

1 Like

This solution gives WA:

Can anyone give any case where it fails? I have tried all possible cases…

I should have solved this question in C++, done in C, wasted time. anyways logic is simple and good question

n and m can not be stored in int. n * m <= 10^12

hell man :\

“Your task is to find the minimum cost of painting the table thus that it can’t be seen thought from the top (there is no cell which is unpainted on every layer)”.

Bold text is a bit misleading! Got WA…:frowning: It meant you can’t leave any layer completely unpainted.

i m getting WA…plz tell me in which cases it is failing :

provide the strong test cases as many of us have failed…

Can be solved easily in C++ using the STL map! the index can be the cost, and the corresponding maximum tiles can be the data, i.e., map[ck]+=tk.

i don’t think so…

1 Like

I have provided test cases for as much codes as I could have done, please check one other thread in the discussions too where I have given some test cases.

please explain the 1st test case in detail. The problem is still unclear to me. what is meant by table should not be seen through top and no two vertical columns should be unpainted?

i am getting wrong answer for, can someone tell me where i am going wrong

For this testcase some accepted solutions are giving incorrect output

2 5 4

4 0

0 4

9 2

1 19

Answer should be 12 but I found one accepted solution which was giving answer as 37!

1 Like

It will be better to choose std::multimap instead of std::map, for the simple reason that the cost can be same for more than one layer.

no need of multimap… you can add tk to the value stored along key ck in a map itself

I think using map is also correct. As we are only interested in cost and no. of tiles that can be painted with that cost.