Weight for weight Time : Space : 1234567891011121314151617181920212223242526272829303132333435#include <bits/stdc++.h>using namespace std;class WeightSort{public: static std::string orderWeight(const std::string &strng) { unordered_map<string, long long> weight; vector<string> S; string now = ""; long long cost = 0; for(auto ch : strng) { if(ch == ' ') { if(now.length()) { weight[now] = cost; S.push_back(now); now = "", cost = 0; } } else now.push_back(ch), cost += ch - '0'; } if(now.length()) { weight[now] = cost; S.push_back(now); now = "", cost = 0; } sort(begin(S), end(S), [&](auto& a, auto& b) { if(weight[a] != weight[b]) return weight[a] < weight[b]; return a < b; }); string res = ""; for(auto s : S) res += s + " "; while(res.length() and res.back() == ' ') res.pop_back(); return res; }};