fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. void naprawTablice();
  7.  
  8. long long a[10000] = {};
  9. long long sumy[10000] = {};
  10. int temp=0;
  11.  
  12. int main()
  13. {
  14. int n;
  15. cin>>n;
  16. for (int x=0; x!=n; x++) cin>>a[x];
  17.  
  18. for (int x=0; x<n;){
  19. if(a[x]<0){
  20. while(a[x]<=0 && x!=n){
  21. sumy[temp]+=a[x];
  22. x++;
  23. }
  24. temp++;
  25.  
  26. }
  27. else{
  28. while(a[x]>=0 && x!=n){
  29. sumy[temp]+=a[x];
  30. x++;
  31. }
  32. temp++;
  33.  
  34. }
  35. }
  36. if(sumy[0]<0)sumy[0]=0;
  37. if(sumy[temp-1]<0)sumy[temp-1]=0;
  38.  
  39. naprawTablice();
  40.  
  41. if(temp<3) cout<<sumy[0]<<endl;
  42. else{
  43. for (int x=0; x<temp && temp!=1; x+=2){
  44.  
  45. if(sumy[x]+sumy[x+1]+sumy[x+2]>max(sumy[x], sumy[x+2])) {
  46. sumy[x]=sumy[x]+sumy[x+1]+sumy[x+2];
  47. sumy[x+1]=0;
  48. sumy[x+2]=0;
  49. naprawTablice();
  50. naprawTablice();
  51. x=-2;
  52. }
  53. //for (int g=0; g!=temp; g++) cout<<sumy[g]<<endl; cout<<endl;
  54. }
  55.  
  56. cout<<*max_element(sumy, sumy+temp)<<endl;
  57. }
  58.  
  59.  
  60. return 0;
  61. }
  62.  
  63. void naprawTablice(){
  64. for (int x=0; x<temp-1; x++){
  65. if(sumy[x]==0){
  66. for (int y=x; y!=temp-1; y++) sumy[y]=sumy[y+1];
  67. temp--;
  68. }
  69. }
  70. if(sumy[temp-1]==0) temp--;
  71. }
  72.  
Success #stdin #stdout 0s 4232KB
stdin
Standard input is empty
stdout
0