Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
774 B
1
Indexable
Never
#define ll long long
class Solution {
public:
    long long largestPerimeter(vector<int>& arr) {
        ll n=arr.size();
        sort(arr.begin(),arr.end());
        vector<ll>pre(n);
        pre[0]=arr[0];
        for(ll i=1;i<n;i++) pre[i]=pre[i-1]+arr[i];

        /* for(int i=0;i<n;i++){
            cout<<i<<" ";
        }
        cout<<"\n";
        for(int i=0;i<n;i++){
            cout<<arr[i]<<" ";
        }
        cout<<"\n";
        for(int i=0;i<n;i++){
            cout<<pre[i]<<" ";
        }
        cout<<"\n"; */

        ll res=INT_MIN;
        ll sides=1;
        for(int i=n-1;i>0;i--){
            if(arr[i]<pre[i-1]){
                res=max(res,pre[i]);
                sides=i+1;
            }
        }
        return (sides>2)?res:-1;
    }
};
Leave a Comment