fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. void naprawTablice();
  7.  
  8. long long a[100002] = {};
  9. long long sumy[100002] = {};
  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;){ //sumowanie wszystkich liczb obok siebie z tym samym znakiem
  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.  
  37. //usuniecie minusowych z początka i końcu, bo odrzucamy to na 100%
  38. if(sumy[0]<0)sumy[0]=0;
  39. if(sumy[temp-1]<0)sumy[temp-1]=0;
  40.  
  41. naprawTablice();
  42.  
  43. if(temp<3) cout<<sumy[0]; //jesli temp<3 po usunieciu liczb ujemnych z poczatka i konca, to na 100% jest tylko jedna liczba dodatnia
  44. else{
  45. for (int x=0; x<temp-2 && temp!=1; x+=2){
  46. if(sumy[x]+sumy[x+1]+sumy[x+2]>=max(sumy[x], sumy[x+2])) {
  47. sumy[x]=sumy[x]+sumy[x+1]+sumy[x+2];
  48. sumy[x+1]=0;
  49. sumy[x+2]=0;
  50. naprawTablice();
  51. x=-2; //zeby bylo zero po skonczeniu cyklu
  52. }
  53. //for (int g=0; g!=temp; g++) cout<<sumy[g]<<endl; cout<<endl;
  54. }
  55.  
  56. cout<<*max_element(sumy, sumy+temp); //wypisujemy najwieksza mozliwa wartosc
  57.  
  58. }
  59. return 0;
  60. }
  61.  
  62. void naprawTablice(){ //czysczenie tablicy z zer
  63.  
  64. bool zmiana=0;
  65. do{
  66. zmiana=0;
  67. for (int x=0; x<temp-1; x++){
  68. if(x>=temp) x=0;
  69. if(sumy[x]==0){
  70. for (int y=x; y!=temp-1; y++) sumy[y]=sumy[y+1];
  71. temp--;
  72. zmiana=1;
  73. }
  74. }
  75.  
  76. if(sumy[temp-1]==0) temp--;
  77. }while(zmiana==1);
  78. }
  79.  
Success #stdin #stdout 0s 4548KB
stdin
2
-1
-2
stdout
Standard output is empty