<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:
    int kConcatenationMaxSum(vector<int>& arr, int k) 
    {
        if(k==1)
        {
            int maxSum = 0;
            int sumTillNow = 0;

            for(int i=0;i<arr.size();i++)
            {
                //add
                sumTillNow += arr[i];
                //update
                maxSum = max(maxSum,sumTillNow);
                //reset
                if(sumTillNow<0) //agar sum negative jaa rha hai to kyu continue kru, wapas se start karte hai
                {
                    sumTillNow = 0;
                }
            }
            return maxSum;
        }

        vector<int> temp = arr;
        for(auto v:arr)
        {
            temp.push_back(v);
        }

        int maxSum = temp[0];
        int sumTillNow = 0;

        for(int i=0;i<temp.size();i++)
        {
            //add
            sumTillNow += temp[i];
            //update
            maxSum = max(maxSum,sumTillNow);
            //reset
            if(sumTillNow<0) //agar sum negative jaa rha hai to kyu continue kru, wapas se start karte hai
            {
                sumTillNow = 0;
            }
        }

        

        int totsum = 0;
        for(auto v:arr)
        {
            totsum += v;
        }
        
        bool allPos = 1;
        for(auto v:arr)
        {
            if(v<0)
            {
                allPos = 0;
                break;
            }
        }
        if(allPos)
        {
            return (1LL*totsum*k)%1000000007;
        }

        bool allNeg = 1;
        for(auto v:arr)
        {
            if(v>0)
            {
                allNeg = 0;
                break;
            }
        }
        if(allNeg)
        {
            return 0;
        }

        if(totsum<0)
        {
            return maxSum%1000000007;
        }
        else if(totsum>=0)
        {
            return ((1LL*(k-2)*totsum)%1000000007+maxSum)%1000000007;
        }
        return arr[0]*k;
    }
};
46700