The way I get this done is dynamic programming . Unlike the greedy method I do a little preliminary stuff to finished it.
the equation is pretty easy , just three way of get next floor.but we have to find the topmost floor that need to go .
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
const int maxn = 1005;
const int INF = 1<<29;
int n = 0,m;
int dp[maxn][2];
int lmost[maxn],rmost[maxn];
int go(int from,int to,int i)
{
if(from == 1 && to == 1)
{
return lmost[i]*2