C++ 5 lines
Find the maximum subbaray sum m_sum for 2-concatenated array (note the special case when k == 1).\n- If the sum of the entire array is positive, add it k - 2 ti
- Find the maximum subbaray sum `m_sum` for 2-concatenated array (note the special case when `k == 1`).\n- If the sum of the entire array is positive, add it `k - 2` times.\n\n> Note that If the sum of the entire array is positive, `m_sum` will always span across two arrays.\n\n```\nint kConcatenationMaxSum(vector<int>& a, int k) {\n    int m_sum = 0, sz = a.size();\n    for (auto i = 0, sum = 0; i < min(2, k) * sz; ++i) {\n        sum = max(sum + a[i % sz], a[i % sz]);\n        m_sum = max(m_sum, sum);\n    }\n    return ((long long)m_sum + \n        max(0ll, accumulate(begin(a), end(a), 0ll) * max(0, k - 2))) % 1000000007;\n}\n```
[]