Explanation
Let be .
Sort the array in ascending order. Kotivalo can read the books in the order , and Justiina can read them in the order .
If , Justiina would finish reading books and would have to wait for Kotivalo to finish reading . Afterwards, Justiina can read and Kotivalo can read . In total, this will take units of time.
If , Kotivalo would finish reading before Justiina finishes reading , so Justiina would not have to wait for Kotivalo to finish reading . Thus, this will take units of time.
Therefore, the answer is .
Implementation
Time Complexity: .
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;int main() {ll n;cin >> n;vector<ll> books(n);
Java
import java.io.*;import java.util.*;class ReadBook {public static void main(String[] args) {Kattio io = new Kattio();int n = io.nextInt();long books[] = new long[n];for (int i = 0; i < n; i++) { books[i] = io.nextLong(); }
Python
n = int(input())books = [int(b) for b in input().split()]longest_time = max(books)total_time = sum(books)print(max(total_time, longest_time * 2))
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!