From 1b33db721a050028023f2efde3eac5a42c1c84d3 Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Mon, 19 Sep 2016 23:10:01 +0300 Subject: [PATCH] maxsubarray: initial commit --- .../dynamic_programming/maxsubarray/README.md | 1 + .../maxsubarray/solution.cpp | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 algorithms/dynamic_programming/maxsubarray/README.md create mode 100644 algorithms/dynamic_programming/maxsubarray/solution.cpp diff --git a/algorithms/dynamic_programming/maxsubarray/README.md b/algorithms/dynamic_programming/maxsubarray/README.md new file mode 100644 index 0000000..c3da2f0 --- /dev/null +++ b/algorithms/dynamic_programming/maxsubarray/README.md @@ -0,0 +1 @@ + diff --git a/algorithms/dynamic_programming/maxsubarray/solution.cpp b/algorithms/dynamic_programming/maxsubarray/solution.cpp new file mode 100644 index 0000000..944e36c --- /dev/null +++ b/algorithms/dynamic_programming/maxsubarray/solution.cpp @@ -0,0 +1,39 @@ +#include +#include + + +int main() +{ + using namespace std; + + size_t T; + cin >> T; + + while (T--) { + + size_t N; + cin >> N; + + int v; + cin >> v; + N--; + + long long max_sum_cont = v; + long long max_sum_cont_tmp = v; + long long max_sum_noncont = v; + + while (N--) { + + cin >> v; + + max_sum_cont_tmp = max((long long)v, max_sum_cont_tmp + v); + max_sum_cont = max(max_sum_cont, max_sum_cont_tmp); + + max_sum_noncont = max((long long)v, max_sum_noncont + (v > 0 ? v : 0)); + } + + cout << max_sum_cont << ' ' << max_sum_noncont << endl; + } + + return 0; +}