fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4. #include <cmath>
  5. #include <locale.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. using namespace std;
  10.  
  11. int *x;
  12. int x2;
  13. int i;
  14. int p1;
  15. int p2;
  16. int i2;
  17. int* m;
  18. int* m2;
  19. int z=1;
  20. int i3;
  21. int i4;
  22.  
  23. int checktop(int c) {
  24. for (i2 = 0, p1 = 0, p2 = 0; i2 < c; i2++, x++)
  25. p1 += *x;
  26. x = m;
  27. return p1;
  28. }
  29.  
  30.  
  31. int checkdown(int c2) {
  32. x = x + (x2 - c2);
  33. for (i2 = 0, p1 = 0, p2 = 0; i2 < c2; i2++, x++)
  34. p1 += *x;
  35. x = m;
  36. return p1;
  37. }
  38.  
  39. int checkall() {
  40. x = m;
  41. for (i2 = 0, p1 = 0, p2 = 0; i2 < x2; i2++,x++) {
  42. p1 += *x;
  43.  
  44. }
  45. x = m;
  46. return p1;
  47. }
  48.  
  49. void cutofftop(int c) {
  50. m = m + c;
  51. x = m;
  52. x2 = x2 - c;
  53. }
  54.  
  55. void cutoffdown(int c) {
  56. x2 = x2 - c;
  57. }
  58.  
  59. int sercz() {
  60. z = 0;
  61. int *xc = m;
  62. int *xc2 = m;
  63. x = m;
  64. int y;
  65. y = checkall();
  66. for (i3 = 1; i3 < (x2+1); i3++, xc++) {
  67. //cout << *xc << endl;
  68. if (*xc < 0) {
  69. if (checkdown(x2 - i3) > y)
  70. if (checktop(i3 - 1) > checkdown(x2 - i3)) {
  71. xc2 += x2;
  72. for (i4 = x2; i4 > i3 + 1; i4++, xc2--) {
  73. if (*xc2 < 0) { cutoffdown(abs(i4 - x2) + 1); z = 1; break; }
  74. }
  75. if (z == 0) {
  76. cutoffdown(x2 - i3 + 1);
  77. z = 1;
  78. break;
  79. }
  80. }
  81. else {
  82. //for (i4 = x2; i4 > i3 + 1; i4++, xc2--) {
  83. // if (*xc2 < 0) { cutoffdown(abs(i4 - x2) + 1); z = 1; break; }
  84. //}
  85. //if (z == 0) {
  86. cutofftop(i3);
  87. z = 1;
  88. break;
  89. }
  90. else if (checktop(i3-1) > y) {
  91. xc2 += x2;
  92. for (i4 = x2; i4 > i3 + 1; i4++, xc2--) {
  93. if (*xc2 < 0) { cutoffdown(abs(i4 - x2) + 1); z = 1; break;}
  94. }
  95. if (z == 0) {
  96. cutoffdown(x2 - i3 + 1);
  97. z = 1;
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. x = m;
  104. return 1;
  105. }
  106.  
  107.  
  108.  
  109.  
  110. int main() {
  111. cin >> x2;
  112. x = new int[x2];
  113. m = x;
  114. m2 = x;
  115. for (i = 0; i < x2; i++, x++) cin.ignore(), cin >> *x;
  116. x = m;
  117. while (z == 1) sercz();
  118. cout << checkall() << endl;
  119. x = m2;
  120. delete[] x;
  121. return 0;
  122. }
Success #stdin #stdout 0s 15240KB
stdin
5
1 
-2
4
5
-2
stdout
9