In this HackerEarth Utkarsh and Distributing Books problem solution Utkarsh's mother recently received N piles of books as a gift from someone. The ith pile contains Bi books.

She neither wants to keep the voluminous books with herself nor she wants to throw them away. So, she decided to distribute them to students of the nearby school. She decided to call K students to her home and ask one of her sons (Utkarsh or Saharsh) to distribute books.

Also, she will get happy only if the following condition is satisfied: For every pile i there must be at least one student who receives more than one book from pile i.

She knows that Utkarsh is very lazy. So he will randomly pick a student and give him a book from any pile. Since he distributes randomly, he might make her sad.

On the other hand, Saharsh is smart and obedient so he will always find a way of distribution (if possible) that will make her happy.

You need to output 2 integers: The maximum value of K

When Utkarsh is asked to distribute the books. His mother must remain happy irrespective of the way of his distribution.

When Saharsh is asked to distribute the books.


HackerEarth Utkarsh and Distributing Books problem solution


HackerEarth Utkarsh and Distributing Books problem solution.

#include <iostream>
#include <cassert>
using namespace std;

int n;
int a[1010];

void solve() {
  cin>>n;
  for (int i = 1; i <= n; i++) {
    cin>>a[i];
    assert(2 <= a[i] && a[i] <= 1000);

  }

  int minA = 1E9, sum;
  for (int i = 1; i <= n; i++) {
    minA = min(minA, a[i] - 1);
      sum += a[i];

  }

  cout<<minA<<" "<<sum - n <<endl;

}

int main()
{
    int test;
    cin>>test;
    while (test--) {
      solve();
    }
    return 0;
}