maximum_subarray_sum_1643.cpp (409B)
1 #include <algorithm> 2 #include <iostream> 3 #include <vector> 4 5 int main() { 6 size_t n; 7 std::cin >> n; 8 std::vector<long long> a(n); 9 for (size_t i = 0; i < n; i++) 10 std::cin >> a[i]; 11 12 long long scur{0}, smax{0}; 13 for (size_t j = 0; j < n; j++) { 14 scur = std::max(0LL, scur + a[j]); 15 smax = std::max(smax, scur); 16 } 17 if (smax == 0) smax = *std::max_element(a.begin(), a.end()); 18 std::cout << smax << "\n"; 19 }