H.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int maxSum(int a[], int n) {
  4. int res = 0;
  5. int cnt = 0;
  6. for (int i = 0; i < n; i++) {
  7. if (cnt > 0) {
  8. cnt += a[i];
  9. } else {
  10. cnt = a[i];
  11. }
  12. res = max(res, cnt);
  13. }
  14. return res;
  15. }
  16. int main() {
  17. int n, x;
  18. cin >> n >> x;
  19. int res = 0;
  20. int a[n];
  21. for(int i = 0; i < n; i++){
  22. cin >> a[i];
  23. }
  24. if (x < 0) {
  25. int b = 0, e = 0, max_b = 0, max_e = 0;
  26. int cnt = 0, s = 0;
  27. for (int i = 0; i < n; i++) {
  28. if (cnt < 0) {
  29. cnt += a[i];
  30. if(cnt <= res){
  31. res = cnt;
  32. max_e = i+1;
  33. max_b = b;
  34. }
  35. } else {
  36. cnt = a[i];
  37. b = i;
  38. if(cnt <= res){
  39. res = cnt;
  40. max_e = i+1;
  41. max_b = b;
  42. }
  43. }
  44. }
  45. if(res < 0) {
  46. for (int i = max_b; i < max_e; i++) {
  47. a[i] *= x;
  48. }
  49. }
  50. res = maxSum(a, n);
  51. }else{
  52. res = maxSum(a, n)*x;
  53. }
  54. cout << res << endl;
  55. }