Reading Books(Sorting and Searching)
题目描述
There are n books, and Kotivalo and Justiina 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?
输入
The first input line has an integer n(1 ≤ n ≤ ): the number of books.
The second line has n integers t1,t2,...,tn(1 ≤ ti ≤ ): the time required to read each book.
输出
Print one integer: the minimum total time.
样例输入
3
2 8 3
样例输出
16
思路分析
阅读所需最短可能时间为所有书籍所需阅读时间的和total。
假设将每本书所需阅读时间按升序排序,A从所需时间最短的书籍开始读,B从所需时间最长的书籍开始阅读。如果A读完了前n-1本书,而B还未读完第n本书,那么两人均读完这n本书所花费的时间就大于total,为t[n]*2。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,t,total;
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>n;vector<ll>t(n,0);for(ll i=0;i<n;i++){cin>>t[i];total+=t[i];}sort(t.begin(),t.end());cout<<max(total,t[n-1]*2);return 0;
}