Advertisement
newb_ie

Untitled

Sep 11th, 2021
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 2000;
  5. long long a[maxN];
  6.  
  7. bool yes (int n,long long hp) {
  8. for (int i = 1; i <= n; ++i) {
  9. hp += a[i];
  10. if (hp < 1) return false;
  11. }
  12. return true;
  13. }
  14.  
  15. int main () {
  16. ios::sync_with_stdio(false);
  17. cin.tie(nullptr);
  18. cout.tie(nullptr);
  19. int T;
  20. cin >> T;
  21. for (int test_case = 1; test_case <= T; ++test_case) {
  22. int n;
  23. cin >> n;
  24. for (int i = 1; i <= n; ++i) cin >> a[i];
  25. long long l = 0,r = 1e9;
  26. long long res = 0;
  27. while (l <= r) {
  28. long long hp = (l + r) / 2;
  29. if (yes(n,hp)) {
  30. res = hp;
  31. r = hp - 1;
  32. } else {
  33. l = hp + 1;
  34. }
  35. }
  36. cout << res << '\n';
  37. }
  38. //~ cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  39. }
  40.  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement