Hi,

I was working on this problom from the USACO guide:

https://cses.fi/problemset/task/1161/

This is my code

```
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
long long N,X;
//get sum closest to c/2
long long index(const vector<long long>& in,long long c ,const vector<long long>& psum){
long long goal=c/2;
auto iter=upper_bound(psum.begin(),psum.end(),goal);
if(iter==psum.end()){
return *--iter;
}
long long upper=*iter;
long long lower=*--iter;
if(abs(lower-goal)>abs(upper-goal)){
return upper;
}
return lower;
}
int main() {
//read input
cin>>N>>X;
vector<long long> in(X);
map<long long,long long> elements;
for(long long i=0;i<X;i++){
long long input;
cin>>input;
in[i]=input;
elements[input]++;
}
//input reading done
sort(in.begin(),in.end());
//calculate prefix sums so that we can calculate the closest numbers in logN time
vector<long long> psum(X+1);
for(int i=1;i<=X;i++){
psum[i]=psum[i-1]+in[i-1];
}
//queue to store the sticks
queue<long long> pq;
pq.push(N);
long long ans=0;
while(!pq.empty()){
long long top=pq.front();
if(elements[top]>0){
//if we have that stick length is one of the desired we can pop it and mark it as used
elements[top]--;
}else{
//break the current stick
long long f=index(in,pq.front(),psum);
pq.push(pq.front()-f);
pq.push(f);
ans+=pq.front();
}
//pop the last one because we broke it
pq.pop();
}
cout<<ans;
return 0;
}
```

The time complexity is X*logX but this is at worst case and most of the time it should be well below this.

these are the results:

I am confused on how my code is timing out and how I can fix it. Can someone please help?

Thanks.