fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int add(int* tab, int beg, int end, int n);
  6.  
  7. int main()
  8. {
  9. int result;
  10. int max = 0;
  11. int cities;
  12. cin >> cities;
  13. if (cities > 1)
  14. {
  15. int *profit = new int[cities];
  16.  
  17. for (int i = 0; i < cities; i++)
  18. {
  19. cin >> profit[i];
  20. }
  21.  
  22. for (int i = 0; i < cities; i++)
  23. {
  24. for (int n = i; n < cities; n++)
  25. {
  26. result = add(profit, i, n, cities);
  27. if (result > max) max = result;
  28. }
  29. }
  30. cout << max;
  31. delete[] profit;
  32.  
  33. }
  34. else if (cities==1)
  35. {
  36. int profit;
  37. cin >> profit;
  38. if (profit > 0) cout << profit;
  39. else cout << 0;
  40. }
  41. else if (cities < 1) cout << 0;
  42.  
  43. return 0;
  44. }
  45.  
  46. int add(int* tab, int beg, int end, int n)
  47. {
  48. static bool done = 0;
  49. static int *sumpref;
  50. if (done == 0)
  51. {
  52. sumpref = new int[n + 1];
  53. sumpref[0] = 0;
  54. for (int i = 0; i < n; i++)
  55. {
  56. sumpref[i + 1] = sumpref[i] + tab[i];
  57. }
  58. done = 1;
  59. }
  60.  
  61.  
  62. if(beg==end && end==n) delete[] sumpref;
  63.  
  64. return sumpref[end+1] - sumpref[beg];
  65. }
Runtime error #stdin #stdout #stderr 3.13s 529920KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc