Total number of stops for an elevator to serve certain people
First of all I'd like to suggest you use a for-loop, since it would be easier to see, that you're going to increment the loop variable in every iteration independent of any conditions. Then you could also exchange your idx variable for i since the first does not convey more or less semantics than the second and it is somewhat unwritten convention. 

Next: Why use a static_cast&lt;int&gt;for incrementing the stop_count? I think you would not need that, if you defined stop_count as a size_t.

Also: Was there some sort of requirement that solution() takes N as an argument? You could get rid of that parameter, since you have the weight-vector, which in turn has a size().

Furthermore: What is the parameter M (number_of_floors) going to be used for? I do not see much use for it other than boundary checking in the program, in a real world scenario no one could press button 10 in a 5-story building anyway.