# WATERMOVE2 - Editorial

**URL:** <https://discuss.codechef.com/t/watermove2-editorial/126034>\
**Category:** editorial\
**Tags:** editorial, start260\
**Created:** [October 14, 2026, 4:30pm UTC](https://discuss.codechef.com/t/watermove2-editorial/126034 "2026-10-14T16:30:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [October 14, 2026, 4:30pm UTC](https://discuss.codechef.com/t/watermove2-editorial/126034/1 "2026-10-14T16:30:47Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/WATERMOVE2)  
[Contest: Division 1](https://www.codechef.com/START260A/problems/WATERMOVE2)  
[Contest: Division 2](https://www.codechef.com/START260B/problems/WATERMOVE2)  
[Contest: Division 3](https://www.codechef.com/START260C/problems/WATERMOVE2)  
[Contest: Division 4](https://www.codechef.com/START260D/problems/WATERMOVE2)

_ **Author:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Tester:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/iceknight1093)

# DIFFICULTY:

TBD

# PREREQUISITES:

None

# PROBLEM:

You have N glasses of water.  
In one move, you can move **all** the water from one glass to another.  
This must be done exactly K times.

Maximize the maximum amount of water in any glass in the end.

# EXPLANATION:

Each operation allows us to combine the water in two glasses.  
So, K operations allows us to combine the water in upto K+1 glasses into a single glass.

We’re aiming to maximize the maximum amount, so it’s optimal to just choose the most voluminous K+1 glasses to combine.  
Thus, the answer is simply the sum of values of the largest K+1 glasses.

This can be computed in \mathcal{O}(NK) time by repeatedly finding and discarding the maximum element; or in \mathcal{O}(N\log N) by sorting and looking at the last K+1 elements.

# TIME COMPLEXITY:

\mathcal{O}(N\log N) per testcase.

# CODE:

> **Editorialist's code (PyPy3)**
>
> ```python
> for _ in range(int(input())):
> n, k = map(int, input().split())
> a = list(map(int, input().split()))
> a.sort(reverse=True)
> print(sum(a[:min(n, k+1)]))
> 
> ```
