Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
5
Indexable
Never
#define ll long long
class Solution {
public:
    int mod = 1e9+7;
    int kConcatenationMaxSum(vector<int>& arr, int k) {
        vector<ll> dup;
        ll n = arr.size();
        ll i = 0;
        while (k--) {
            i = 0;
            while (i < n) {
                dup.push_back(arr[i]);
                i++;
            }
        }

        ll countp = 0;
        for (ll x : dup) countp += x >= 0; // Use dup instead of arr

        if (countp == dup.size()) { // Use dup.size() instead of n
            ll sum = 0;
            for (ll x : dup) sum += x%mod;
            return sum%mod;
        }

        ll countn = count_if(begin(dup), end(dup), [](ll& x) {
            return x < 0;
        });

        if (countn == dup.size()) return 0; // Use dup.size() instead of n

        ll curr_sum = 0, max_sum = INT_MIN;
        for (ll x : dup) {
            curr_sum += x%mod;
            if (curr_sum < 0) curr_sum = 0;
            max_sum = max(max_sum, curr_sum);
        }
        return max_sum%mod;
    }
};