➤ Problem Link : 525C. Ilya and Sticks
✅ C++ Solution :
#include<bits/stdc++.h> using namespace std; #define ll long long int int main() { int n,val; cin>>n; vector<ll> v; vector<ll> pv; for(int i=0;i<n;i++) { cin>>val; v.push_back(val); } sort(v.begin(),v.end(),greater<ll>()); ll ans=0; ll l=-1; int i=0; while(i<n) { if(l==-1) l=v[i]; else if(v[i]==l) { pv.push_back(l); l=-1; } else if(v[i]==l-1) { pv.push_back(v[i]); l=-1; } else l=v[i]; i++; } sort(pv.begin(),pv.end(),greater<ll>()); i=0; while(i+1 < pv.size()) { ans+=pv[i]*pv[i+1]; i+=2; } cout<<ans; }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!