 # GUZAC - EDITORIAL

Setter: Aleksa Plavsic
Tester: Hussain
Editorialist: Taranpreet Singh

Easy

Math, Greedy

### PROBLEM:

Given N, number of elements on sequence P, and first K elements and Maximum difference between Maximum and minimum element of sequence P, find the maximum possible sum of this sequence. All elements of P should be unique.

### QUICK EXPLANATION

•   Find minimum element in first $K$ elements. Maximum difference $X$ implies that Maximum element of this sequence can be at most $min+X$.

•   We can greedily choose values from $min+X$ to $min$ not already in $P$, choosing larger values first so as to maximize sum.

•   After choosing $N-K$ new elements, our $P$ contains $N$ elements, and sum of all these elements is answer.

•   Checking every single number will take at least $N-K$ iterations which will time out. So, we will add multiple numbers simultaneously.


### EXPLANATION

First of all, we can add all these K elements to an array and sort them. Minimum element is A. Since maximum difference between smallest and largest element is X, so, Maximum element should be A+X. It does not make sense to choose a number < A as this would reduce the maximum choosable element, reducing the final sum.

Now, Brute force solution is to check every element from A+X to A will we find N-K more elements, adding the element if it doesn not already exist in P. This solution has complexity of order O(N) which will Time out.

Now, to get solution of order O(K), see that consecutive pair of elements each represent a range of elements not yet chosen.

If A*-A[i-1]>1 for every 2 \leq i \leq K , we can choose any number in range [A[i-1]+1, A*-1]. Using the formula for summation over range of numbers, we can add all these elements at once in O(1), reducing overall time complexity to O(K).

Boundary case need to be handled separately. It occurs when A[K-1] < A+X.

4 2 4 1 4

In above case, we can choose 3rd element as 5 and 4th element as 3.

To handle this, we can simply add fictitious element A+X+1 at end of sequence (Remember not to add this to answer.)

So, our final solution looks like

Iterate from right to left, add set of number using the formula. Carefully handle case when Length of segment is more than Number of elements remaining to be added, Say R. In this case, only add largest R numbers in current range.

Have a look at solutions below if any doubt, and feel free to ask in comments.

Complexity:
Time complexity of this solution is O(T*K*logK).

Similar problem

Minimize sum instead of maximize. Same constraints. But, it is not guaranteed that solution always exist.

Also, Find conditions when solution doesn’t exist.

### AUTHOR’S AND TESTER’S SOLUTIONS:

Feel free to Share your approach, If it differs. Suggestions are always welcomed. 1 Like

The problem says that pi is not greater than 10^9. But in actual Sequence pi is greater than 10^9. I think this is wrong statement. where pi is the number of candies given to the students.
It is not mentioned anywhere that 0 < i <= k.
so 0 < i <= n is also valid.

Tried the question with the brute force approach and succeeded .But i not quite getting the approach written here and didn’t all the 3 codes provided here.Can someone simplify the approach written here.

Complexity is O(T*KlogK) …Sorting…

2 Likes

Added Fictitious element should be A+X and not A+X+1. Also in the given problem to maximize the S there can be possibility that solution does not exist it will be when A+X < N in this case you will only have N A+X elements to keep in the final array.

ex:

7 3 5
1 2 3

maximum element in the ans array will be 6 but we don’t have 3 elements less than 6 other than 1 2 and 3

My solution is getting RUNTIME ERROR can some one tell me the mistake https://www.codechef.com/viewsolution/19799361

The editorial says that the maximum value possible can be A + X but the constraints says that pi<=1e9 and what if A + X > 1e9, then we need to reduce the minimum value in order to satisfy the constraints. Correct me if I am wrong !

In the question it was mentioned that Pi is <=10^9 for all valid i. Now by “valid i”,I thought that i is in the range of 1 to N, but it seems that it was only for first K elements and remaining numbers could be greater than 10^9. My solution got rejected just because of this ambiguity Tricky case :
1
10 3 49
1 43 44

Output:
415

My approach :

1. find the maximum P a = Smallest + x = 50
2. find the required no of positions to be filled = k - n = 7
3. find the smallest b of the continuous integers you need to find in the formula. b = 50 - (n-1-k) = 50 - (10 - 1 - 3) = 50 - 6 = 45.
4. That is , for remaining 7 elements , you needed sum of
A[0…2] + 50 + 49+ 48 + 47 + 46 + 45 + 44.
5. now since , this (44) was already present in A[k]s , instead of 44 , you can take 43…but is 43 is also present…take 42.
6. now we can find tot = Sum(A[k]s) + Sum(a…b)
2 Likes

What is the formula for summation over a range?

can anyone give me a test case in which my solution is not working ?
It’s passing in all the test cases given here and sample test cases.
here’s my solution getting WA.
https://www.codechef.com/viewsolution/19811696

Please explain how to process multiple elements in a range simultaneously. I am not getting.

Can anyone help me to rectify where my program is failing

[24, 146] N=371 k=2 x=237
what should be output ?
There is a mistake in constraints x must be greater than N otherwise There won’t be any case possible where all are distinct and still difference between maximum and minimum does not exceed.
( I strongly believe the tester has added that cases otherwise my codes is giving correct for every valid input).
Here is the code :- https://www.codechef.com/viewsolution/19811696

hey i did it with same strategy but it is running out of time(TLE), i have checked it thoroughly, please somebody look into it https://ide.codingblocks.com/#/s/21991 (commented code) , https://www.codechef.com/viewsolution/19797503(submission)… if have any test cases please give them

what if the test case is …
4 2 4
1e9-2,1e9-2

in that case number might be greater than 1e9 which is the contradiction of the given constraints ??

i dont know why i am getting wrong answer ,i checked with all test cases help me @taran_1407
enter code here#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,k,x,i;
long long int sum,mi,req,maxval;
cin>>t;
while(t–)
{
sum=0;
mi=INT_MAX;
cin>>n>>k>>x;
vectorv;
long long int a[k];
for(i=0;i<k;i++)
{
cin>>a*;
sum+=a*;
v.push_back(a*);
mi=min(mi,a*);
}
maxval=mi+x;

sort(v.rbegin(),v.rend());


req=n-k;
if(maxval-v<=req)
{
req-=(maxval-v);
sum+=((maxval-v)*(maxval+v+1))/2;

    }
else if(maxval-v>req)
{
sum+=((req)*(maxval+maxval-req+1))/2;
req=0;
}

for(i=0;i<v.size()-1&&req>0;i++)
{
if(v*-v[i+1]-1<=req)
{
req-=(v*-v[i+1]-1);
sum+=((v*-v[i+1]-1)*(v*+v[i+1]))/2;
}
else if(v*-v[i+1]-1>req)
{
sum+=((req)*(v*-1+v*-req))/2;
req=0;
}

}
cout<<sum<<"


";

}
}

Entertain the possibility that the minimum number can be some number less than minimum of the pi’s;
Test Case -
1
13 4 10
4 5 9 13

Please anyone take a look at my code and tell me where am I doing it wrong?

ll t;scanf("%lld",&t);
while(t--)
{
ll n,k,x;scanf("%lld %lld %lld",&n,&k,&x);
ll a[k+1];
ll s=0,cou=k;
f(k)
{
scanf("%lld",&a*);
s+=a*;
}
sort(a,a+k);
a[k]=a+x+1;
repb(i,k,1)
{
if(a*-a[i-1]>1)
{
if((cou+a*-1-a[i-1])<=n)
{
s+=(a**(a*-1))/2;
s-=(a[i-1]*(a[i-1]+1))/2;
cou+=a*-a[i-1]-1;
}
else
{
s+=(a**(a*-1))/2;
ll need=n-cou;
need=a*-1-need;
s-=(need*(need+1))/2;
}
}

}
printf("%lld


",s);
}

The aim og adding fictitious element depend upon the implementation.

Yes min+X can also be used.

See editorialist solution. I first add all K elements. That’s why i use exclusive bounds in my solution. See editorialist solution.