There are n books, and Kotivalo and Justina are going to read them all. For each book, you know the time it takes to read it.
They both read each book from beginning to end, and they cannot read a book at the same time. What is the minimum total time required?
Example:
Input: n = 3, arr = {2, 8, 3}
Output: 16
Approach
C++
#include <bits/stdc++.h>using namespace std;long long readingBooks(long long n,vector<long long> &arr){long long sum = 0, mx = 0;for (long long i = 0; i < n; i++){sum += arr[i];mx = max(mx, arr[i]);}return max(sum, 2 * mx);}int main(){long long n = 3;vector<long long> arr = {2, 8, 3};cout << readingBooks(n, arr) << "\n";return 0;}
No comments:
Post a Comment