<p>Given an integer array <code>arr</code> and an integer <code>k</code>, modify the array by repeating it <code>k</code> times.</p>

<p>For example, if <code>arr = [1, 2]</code> and <code>k = 3 </code>then the modified array will be <code>[1, 2, 1, 2, 1, 2]</code>.</p>

<p>Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be <code>0</code> and its sum in that case is <code>0</code>.</p>

<p>As the answer can be very large, return the answer <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,2], k = 3
<strong>Output:</strong> 9
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,-2,1], k = 5
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [-1,-2], k = 7
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= arr[i] &lt;= 10<sup>4</sup></code></li>
</ul>

// class Solution {
// public:
//     long long int mod = 1e9;
//     int kConcatenationMaxSum(vector<int>& arr, int k) {
//         int n = arr.size();
//         long long int tot,psum=arr[0],ssum=arr[n-1];
//         vector<long long int>pre(n),suff(n);
//         pre[0]=arr[0];
//         suff[n-1]=arr[n-1];
//         for(int i=1;i<n;i++){
//             pre[i]=pre[i-1]+arr[i];
//             suff[n-i-1]=suff[n-i]+arr[n-i-1];
//             psum=max(psum,pre[i]);
//             ssum=max(ssum,suff[n-i-1]);
//         }
//         tot=pre[n-1];
//         vector<long long int>dp(n+1);
//         dp[0]=arr[0];
//         long long int maxsum=dp[0];
//         for(int i=1;i<n;i++){
//             dp[i]=max((long long int)arr[i],dp[i-1]+arr[i]);
//             maxsum=max(maxsum,dp[i]);
//         }
//         cout<<psum<<" "<<tot<<" "<<ssum<<" "<<maxsum<<endl;
//         long long int psum2=psum,ssum2=ssum,tot2=tot,maxsum2=maxsum;
//         long long int psum3,ssum3,maxsum3,tot3;
//         k--;
//         while(k--){
//             psum3=max(psum,(tot+psum2)%mod);
//             ssum3=max(ssum2,(tot2+ssum)%mod);
//             tot3=(tot2+tot)%mod;
//             maxsum3=max({maxsum2,maxsum,(ssum+psum2)%mod});
//             psum=psum3;
//             ssum=ssum3;
//             tot=tot3;
//             maxsum=maxsum3;
//         }
//         if(maxsum<0)return 0;

//         return maxsum%mod;
//     }
// };

class Solution {
public:
    int mod=1e9+7;
    int kConcatenationMaxSum(vector<int>& a, int k) {
        int n=a.size(),sum=a[0],msum=a[0],ans=max(0,a[0]);
        vector<int>dp(n);
        dp[0]=a[0];
        for(int i=1;i<n;i++){
            dp[i]=max(a[i],dp[i-1]+a[i]);
            sum+=a[i];
            msum=max(msum,sum);
            ans=max(ans,dp[i]);
        }
        if(k==1){
            return ans;
        }else{
            if(sum>0){
                return (((long long)(k-2)*sum)%mod+dp[n-1]+msum)%mod;
            }else{
                for(int i=0;i<n;i++)a.push_back(a[i]);
               n=a.size(),ans=max(0,a[0]);
               dp.resize(n);
               dp[0]=a[0];
                for(int i=1;i<n;i++){
                    dp[i]=max(a[i],dp[i-1]+a[i]);
                    ans=max(ans,dp[i]);
                }
                return ans;
            }
        }
        return 0;
    }
};

46100