Russian writers are known worldwide. One of the main Russian authors is Fyódor Dostoyévskiy. The influence of his works outgrew literature itself and reached other areas such as Philosophy, Psychology, and Theology. His first successful book was "Poor Folk", published in 1846.
This broad and impacting influence is due in part to the deep psychological description of his characters. However, some have criticized him for writing stories which are too long and even weary. Even though he is considered a great writer, Dostoyévskiy's life was not easy. In many occasions he had to write under pressure to make a living and perhaps his prolificacy came from such moments of needs.
Dostoyévskiy's only way to obtain money is by selling chapters of his books. Moreover, he knows the editor would buy each and every chapter he wrote — after all, he is a great writer. However, he wants more than just selling all the chapters he is writing. He noted that his financial woes are cause by his lack of financial discipline. That is why Dostoyévskiy decided that whenever he needs to pay his debts, he will choose some chapters to sell so that the money earned is exactly the amount owed. In this way, he will never have any savings and he will have no need to be disciplined about spending.
Of course he understands there could be some debts he will not be able to pay if he insists on this rule. Thus, knowing the value in rubles for each chapter he already wrote, one of the questions he raised was: what is the smallest debt that he would not be able to pay with previously written chapters? Your task is to answer this question.
Input
You should read an integer (n) in the first line, the number of chapters already written by Dostoyévskiy. The second line has (n) integers, p1,p2,…,pnp1,p2,…,pn, the price in rubles for each of the (n) chapters.
Constraints
- 1≤n≤5⋅1051≤n≤5⋅105.
- 1≤pi≤10121≤pi≤1012, for all ii.
Output
A single positive integer, the value of the smallest amount of debt as described above.
Examples
3 2 1 10000000000
4
1 2
1
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 long long n, sum, a[500005], i; 6 7 int main() 8 { 9 scanf("%lld", &n); 10 for(i=0;i<n;i++) 11 { 12 scanf("%lld", &a[i]); 13 } 14 sort(a, a+n); 15 sum = 0; 16 for(i=0;i<n;i++) 17 { 18 if(a[i]>sum+1) break; 19 sum += a[i]; 20 } 21 printf("%lld ", sum+1); 22 return 0; 23 }