typedef long long ll; typedef long double ld; #include using namespace std; #define int long long signed main(){ ll h,w; std::cin >> h>>w; vector>> prev(h,vector>(w,{-1,-1})); vector s(h); for (int i = 0; i < h; i++) { std::cin >> s[i]; } priority_queue,vector>,greater>> pq; if(s[0][1]>s[1][0]){ pq.push({1, s[1][0],1}); prev[1][0] = {0,0}; }else if(s[1][0]>s[0][1]){ pq.push({1, s[0][1],0}); prev[0][1] = {0,0}; }else{ pq.push({1, s[1][0],1}); pq.push({1, s[1][0],1}); prev[0][1] = {0,0}; prev[1][0] = {0,0}; } vector minc(h+w+2,1000); while(!pq.empty()){ auto [now, ch, y] = pq.top();pq.pop(); // std::cout << now<<" "< now = {h-1,w-1}; while(true){ ans += s[now.first][now.second]; if(now.first==0&&now.second==0)break; now = prev[now.first][now.second]; } reverse(ans.begin(),ans.end()); std::cout << ans << std::endl; }