C++ O(N+M) solution.
\nclass Solution {\npublic:\n\tstring kthSmallestPath(vector<int>& arr, int k) {\n\t\tconst int n = arr[0], m = arr[1];\n\t\tlong long x = m;\n\t\tfor(int i = m
```\nclass Solution {\npublic:\n\tstring kthSmallestPath(vector<int>& arr, int k) {\n\t\tconst int n = arr[0], m = arr[1];\n\t\tlong long x = m;\n\t\tfor(int i = m + 1; i <= n + m; i++)\n\t\t\tx = x * i / (i - m);\n\t\tstring ans(n + m, \'V\');\n\t\tfor(int i = 0, a = m, b = n; i < n + m; i++) {\n\t\t\tx /= a + b;\n\t\t\tif(a && k <= x)\n\t\t\t\tans[i] = \'H\', x *= --a;\n\t\t\telse\n\t\t\t\tk -= x, x *= b--;\n\t\t}\n\t\treturn move(ans);\n\t}\n};\n```
[]