Simple beginner-level C solution || Beats 100% in Runtime and Memory || Sorting + Prefix Sum
\n# Complexity\n- Time complexity:  O(nlogn)\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n) \n\n# Code\ncpp\n// Runtime 111 ms Beats 100%
\n# Complexity\n- Time complexity:  ```O(nlogn)```\n- Space complexity: ```O(n)```\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp\n// Runtime 111 ms Beats 100% Memory 17.5 MB Beats 100%\nint ascend(const void* pa, const void* pb) {\n    const int a = *(const int*)pa;\n    const int b = *(const int*)pb;\n    if (a == b) {\n        return 0;\n    }\n    return (a < b) ? -1 : 1;\n}\nint sumDistance(int* nums, int numsSize, char* s, int d) {\n    // Sorting + Prefix Sum\n\n    // Time complexity: O(nlogn)\n    // Space complexity: O(n)\n    \n   if (strlen(s) != numsSize) return -1;\n\n    const int mod = 1e9 + 7;\n    const int n = numsSize;\n    int result = 0, prefix = 0;\n    \n    int* pos = (int*)calloc(n, sizeof(int));\n    for (int i = 0; i < n; ++i) {\n        if (s[i] == \'L\') {\n            pos[i] = nums[i] - d;\n        }\n        else pos[i] = nums[i] + d;\n    }\n\n    qsort(pos, n, sizeof(int), ascend);\n\n    for (int i = 0; i < n; ++i) {\n        result = (result + i * pos[i] - prefix) % mod;\n        prefix = (prefix + pos[i]) % mod;\n    }\n    \n    free(pos);\n    return result;\n}\n```
['Array', 'C', 'Sorting', 'Prefix Sum']