<p>You are given an array <code>target</code> of n integers. From a starting array <code>arr</code> consisting of <code>n</code> 1&#39;s, you may perform the following procedure :</p>

<ul>
	<li>let <code>x</code> be the sum of all elements currently in your array.</li>
	<li>choose index <code>i</code>, such that <code>0 &lt;= i &lt; n</code> and set the value of <code>arr</code> at index <code>i</code> to <code>x</code>.</li>
	<li>You may repeat this procedure as many times as needed.</li>
</ul>

<p>Return <code>true</code> <em>if it is possible to construct the</em> <code>target</code> <em>array from</em> <code>arr</code><em>, otherwise, return</em> <code>false</code>.</p>

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

<pre>
<strong>Input:</strong> target = [9,3,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> Start with arr = [1, 1, 1] 
[1, 1, 1], sum = 3 choose index 1
[1, 3, 1], sum = 5 choose index 2
[1, 3, 5], sum = 9 choose index 0
[9, 3, 5] Done
</pre>

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

<pre>
<strong>Input:</strong> target = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Impossible to create target array from [1,1,1,1].
</pre>

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

<pre>
<strong>Input:</strong> target = [8,5]
<strong>Output:</strong> true
</pre>

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

<ul>
	<li><code>n == target.length</code></li>
	<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= target[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    bool isPossible(vector<int>& target) {
        priority_queue<long long> pq;
        long long maxi=0;
        long long indi=-1;
        for(int i=0;i<target.size();i++){
            long long x=target[i];
            pq.push(x);
            if(x>maxi){
                indi=i;
                maxi=x;
            }
        }
        int a=1;
        long long sum=0;
        for(int i=0;i<target.size();i++){
            if(i!=indi){
                sum+=target[i];
            }
        }
        if(target.size()==1){
            if(target[0]!=1){
                a=0;
            }
        }

        
        while(pq.top()>1 && a){

            if(pq.top()<=sum){
                a=0;
                break;
            }
            long long y=pq.top();
            pq.pop();
            long long x=pq.top();
            long long dif=y-x;
            if(y<=sum){
                a=0;
                break;
            }
            if((dif)%sum){
                long long q=dif/sum;
                q++;
                if(y-(q*sum)>0){
                    y=y-(q*sum);
                    pq.push(y);
                    sum-=x;
                    sum+=y;
                }
                else{
                    a=0;
                    break;
                }
            }
            else{
                long long q=dif/sum;
                if((y-(q*sum))==1){
                    y=y-(q*sum);
                    sum-=x;
                    sum+=y;
                    pq.push(y);

                }
                else{
                    a=0;
                    break;
                }
            }


        }
        return a;
        
        
    }
};
36100