Table of Contents

ExplanationImplementation

Unofficial Editorial (C++)

Explanation

Let sum\texttt{sum} be i=1nti\sum\limits_{i=1}^n t_i.

Sort the array in ascending order. Kotivalo can read the books in the order tn,t1,t2,...,tn1t_n, t_1, t_2, ..., t_{n-1}, and Justiina can read them in the order t1,t2,t3,...,tnt_1, t_2, t_3, ..., t_n.

If sumtn<tn\texttt{sum} - t_n < t_n, Justiina would finish reading books t1,t2,t3,...,tn1t_1, t_2, t_3, ..., t_{n-1} and would have to wait for Kotivalo to finish reading tnt_n. Afterwards, Justiina can read tnt_n and Kotivalo can read t1,t2,t3,...,tn1t_1, t_2, t_3, ..., t_{n-1}. In total, this will take 2×tn2 \times t_n units of time.

If sumtntn\texttt{sum} - t_n \ge t_n, Kotivalo would finish reading tnt_n before Justiina finishes reading t1,t2,t3,...,tn1t_1, t_2, t_3, ..., t_{n-1}, so Justiina would not have to wait for Kotivalo to finish reading tnt_n. Thus, this will take sum\texttt{sum} units of time.

Therefore, the answer is max(sum,2×tn)\max(\texttt{sum}, 2 \times t_n).

Implementation

Time Complexity: O(N)\mathcal O(N).

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!