1. Abridged Problem Statement
----------------------------------------------------------------
We have a string of n pearls in a row; initially the first n–m pearls lie on a table, the last m pearls hang off the table. Pearl i has weight w[i] and value c[i]. Each day you may remove exactly one pearl—either from the hanging end (an "H" operation) or from the table end (a "T" operation). When you remove from the hanging end, you steal that pearl (gain its value) but must slide the string so exactly m pearls still hang: one more pearl from the table side becomes hanging (that pearl is not stolen). When you remove from the table end, you simply steal that pearl (gain its value) and hanging pearls stay the same. After every operation, the configuration must satisfy
   (sum of weights of hanging pearls) ≤ k × (sum of weights of pearls on the table),
or else the string will slip. Maximize the total value stolen, and output the number of operations, that maximum total value, and any sequence of 'H'/'T' operations achieving it.

2. Detailed Editorial
------------------------
Definitions and observations
  Let n, m, k be as given. Label pearls 0..n–1, where 0..n–m–1 lie on the table and n–m..n–1 hang.
  An "H" operation: remove pearl at index end (n–1), gain c[n–1], decrease n by 1; then slide: one pearl from the old table end (index n–m–1 before removal) becomes hanging, but is not stolen. Net effect on weights:
      H_weight_lost = w[n–1],
      Table_weight_lost = w[n–m–1],
      Hanging_weight_gained = –w[n–1] + w[n–m–1],
      Table_weight_gained = –w[n–m–1].
  A "T" operation: remove pearl at index 0, gain c[0], shift all indices down by one; hanging segment unaffected.
  The safety constraint only matters when you do an "H" operation, because taking from the table only reduces table weight (and doesn't change hanging), so it cannot immediately break the inequality unless you later do another "H". Hence:
      – If you plan t total "H" operations, you should do them all first (when your table weight is as large as possible), and only after those t operations do you do any number of "T" operations.
      – You only need to check the inequality for each prefix of those t hanging removals.

Prefix sums
  Compute two prefix-sum arrays of length n:
      pref_w[i] = w[0] + w[1] + … + w[i],
      pref_score[i] = c[0] + c[1] + … + c[i].
  Define get_weight(l,r) and get_score(l,r) as range-sum queries using these arrays.

Iterating over the remaining string length
  Rather than directly iterating t (number of H's), the solution iterates `len` from n down to m+1, where len = n – t is the remaining string length after t H operations. For each len:
    The hanging pearls are indices [len–m .. len–1] and the table pearls are [0 .. len–m–1].
    Safety condition: get_weight(len–m, len–1) ≤ k × get_weight(0, len–m–1).
    If unsafe, stop (further reductions of len only make the table lighter).
  Within each feasible len, we can take T operations (steal from the front). Using binary search over the left cut position, find the largest number of T's such that the balance still holds: find the largest `ans_pos` in [0..len–m–1] such that get_weight(ans_pos, len–m–1) × k ≥ weight_hanging. The score from T's is get_score(0, ans_pos–1).

Scoring
  `add` accumulates the values of pearls taken from the hanging end (as len decreases, we add a[len–1].second to add after processing len).
  Total(len) = get_score_from_left(len) + add. Track the best total and record best_len.

Reconstruct
  Output (n – best_len) 'H' characters (for each i from best_len to n–1) followed by as many 'T' characters as the balance allows (greedily adding T's from the left while balance holds).

Time complexity O(n log n) for the binary searches over the left cut, with O(n) enumeration of len.

3. C++ Solution
-------------------------------------------------------------
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, m, k;
vector<pair<int, int>> a;

void read() {
    cin >> n >> m >> k;
    a.resize(n);
    cin >> a;
}

void solve() {
    // Pearls 1..n with prefix sums over weight and price. Taking a hanging
    // pearl (H) drops the rightmost remaining pearl; taking a table pearl (T)
    // drops the leftmost remaining one. After all H moves the remaining pearls
    // are a prefix 1..len, and the hanging part is the last m of them; balance
    // requires weight(len-m..len-1) <= k * weight(remaining table pearls).
    //
    // We iterate len from n down to m+1, i.e. over the number of H pearls
    // taken (their prices accumulate in `add`). For each len the hanging
    // weight is fixed, so the most table pearls we may strip from the left is
    // monotone: binary search the largest left cut still keeping balance and
    // add the price of those table pearls. We keep the (len, total) giving the
    // best money.
    //
    // Finally we reconstruct: H characters for every pearl taken from the
    // right, then T characters for table pearls peeled from the left while the
    // balance condition still holds.

    vector<int> pref_w(n), pref_score(n);
    for(int i = 0; i < n; i++) {
        pref_w[i] = a[i].first;
        pref_score[i] = a[i].second;
        if(i > 0) {
            pref_w[i] += pref_w[i - 1];
            pref_score[i] += pref_score[i - 1];
        }
    }

    auto get_weight = [&](int l, int r) -> int {
        if(l > r) {
            return 0;
        }
        return pref_w[r] - (l > 0 ? pref_w[l - 1] : 0);
    };

    auto get_score = [&](int l, int r) -> int {
        if(l > r) {
            return 0;
        }
        return pref_score[r] - (l > 0 ? pref_score[l - 1] : 0);
    };

    auto get_score_from_left = [&](int len) -> int {
        int low = 0, high = len - m - 1, ans_pos = -1;
        int weight_hanging = get_weight(len - m, len - 1);
        while(low <= high) {
            int mid = (low + high) / 2;
            if(get_weight(mid, len - m - 1) * k >= weight_hanging) {
                ans_pos = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }

        assert(ans_pos != -1);
        return get_score(0, ans_pos - 1);
    };

    int ans = 0, add = 0, best_len = 0;
    for(int len = n; len > m; len--) {
        if(get_weight(len - m, len - 1) > k * get_weight(0, len - m - 1)) {
            break;
        }

        int new_score = get_score_from_left(len) + add;
        if(new_score > ans) {
            ans = new_score;
            best_len = len;
        }

        add += a[len - 1].second;
    }

    if(ans == 0) {
        cout << 0 << ' ' << 0 << '\n';
        return;
    }

    string ans_str;
    for(int i = best_len; i < n; i++) {
        ans_str.push_back('H');
    }

    for(int l = 0; l < best_len; l++) {
        if(get_weight(best_len - m, best_len - 1) >
           k * get_weight(l + 1, best_len - m - 1)) {
            break;
        }

        ans_str.push_back('T');
    }

    cout << ans_str.size() << ' ' << ans << '\n';
    cout << ans_str << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

4. Python Solution
---------------------------------------------
```python
import sys
import threading
def main():
    data = sys.stdin.read().split()
    if not data:
        return
    it = iter(data)
    n = int(next(it))
    m = int(next(it))
    k = int(next(it))
    w = [0]*n
    c = [0]*n
    for i in range(n):
        w[i] = int(next(it))
        c[i] = int(next(it))

    # Build prefix sums
    prefW = [0]*n
    prefC = [0]*n
    prefW[0] = w[0]
    prefC[0] = c[0]
    for i in range(1,n):
        prefW[i] = prefW[i-1] + w[i]
        prefC[i] = prefC[i-1] + c[i]

    # Helpers: sum of w[l..r], sum of c[l..r]
    def getW(l, r):
        if l > r:
            return 0
        return prefW[r] - (prefW[l-1] if l>0 else 0)
    def getC(l, r):
        if l > r:
            return 0
        return prefC[r] - (prefC[l-1] if l>0 else 0)

    best_total = 0
    best_x = 0  # number of H's
    best_y = 0  # number of T's

    scoreH = 0  # cumulative sum of H-values stolen so far
    # Try x = 0 .. up to n-m H-operations
    for x in range(0, n-m+1):
        L = n - x       # length of the remaining string
        if L < m:
            break

        # compute weight of hanging and table
        W_hang = getW(L-m, L-1)
        W_table = getW(0, L-m-1)

        # safety check for the last H we did
        if W_hang > k * W_table:
            break

        # Binary search maximum y in [0..L-m] such that
        # getW(y, L-m-1)*k >= W_hang
        lo, hi = 0, L-m
        best_mid = 0
        while lo <= hi:
            mid = (lo + hi)//2
            if getW(mid, L-m-1) * k >= W_hang:
                best_mid = mid
                lo = mid + 1
            else:
                hi = mid - 1
        y = best_mid

        total = scoreH + getC(0, y-1)
        if total > best_total:
            best_total = total
            best_x = x
            best_y = y

        # prepare scoreH for x+1 by adding the next H-pearl's value
        if x < n:
            scoreH += c[n-1-x]

    # print answer
    if best_total == 0:
        print("0 0")
        return
    p = best_x + best_y
    print(p, best_total)
    # x times 'H' then y times 'T'
    print("H"*best_x + "T"*best_y)

if __name__ == "__main__":
    threading.Thread(target=main).start()
```

5. Compressed Editorial
-------------------------
We must steal pearls from either end of an array of n pearls so that after each hanging-end removal, the hanging-weight ≤ k × table-weight. Show that an optimal sequence is: do all H-(hang) removals first (as many as the safety constraint allows), then do as many T-(table) removals as still possible. Precompute prefix sums of weights and values. Iterate over the remaining string length len from n down to m+1 (equivalently, t = n–len H's taken); for each feasible len, binary-search in O(log n) how many T's you can still do. Keep the best total value = sum of stolen hanging values + sum of first y table values. Reconstruct by printing (n–best_len) 'H's then y 'T's. Total time O(n log n).
