[Java/C++/Python] Ants Trick
Explanation\nRobots has no different,\nso we can assume rebots can go cross each other.\nUsualy it\'s a trick for ants, here is robot.\n\nThen sort A.\nFor A[i]
# **Explanation**\nRobots has no different,\nso we can assume rebots can go cross each other.\nUsualy it\'s a trick for ants, here is robot.\n\nThen sort `A`.\nFor `A[i]`,\nit contributes as `A[j] - A[i]` for `i + 1` times, where `j < i`\nit contributes as `A[i] - A[j]` for `n - i` times, where `i < j`.\nSo we count the sum of `A[i] * (1 + i + i - n)`\n<br>\n\n# **Complexity**\nTime `O(sort)`\nSpace `O(sort)`\n<br>\n\n**Java**\n```java\n    public int sumDistance(int[] A, String s, int d) {\n        int n = A.length, mod = (int)1e9 + 7;\n        for(int i = 0; i < n; ++i)\n            A[i] += s.charAt(i) == \'R\' ? d : -d;\n        Arrays.sort(A);\n        long res = 0;\n        for(int i = 0; i < n; ++i)\n            res = (res + (1L + i + i - n) * A[i]) % mod;\n        return (int)(res + mod) % mod;\n    }\n```\n\n**C++**\n```cpp\n    int sumDistance(vector<int>& A, string s, int d) {\n        int n = A.size(), mod = 1e9 + 7;\n        for(int i = 0; i < n; ++i)\n            A[i] += s[i] == \'R\' ? d : -d;\n        sort(A.begin(), A.end());\n        long long res = 0;\n        for(int i = 0; i < n; ++i)\n            res = (res + (1L + i + i - n) * A[i]) % mod;\n        return (res + mod) % mod;\n    }\n```\n\n**Python**\n```py\n    def sumDistance(self, A: List[int], s: str, d: int) -> int:\n        n = len(A)\n        B = sorted(A[i] + (d if s[i] == \'R\' else -d) for i in range(n))\n        return sum((i + i + 1 - n) * a for i,a in enumerate(B)) % (10 ** 9 + 7)\n```\n
['C', 'Python', 'Java']