Sum of Differences

AtCoder
IDabc437_d
Time2000ms
Memory256MB
Difficulty
You are given a length-$N$ sequence of positive integers $A = (A_1, A_2, \dots, A_N)$ and a length-$M$ sequence of positive integers $B = (B_1, B_2, \dots, B_M)$. Find the value of $\displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} |A_i - B_j|$, modulo $998244353$. ## Constraints * $1 \leq N,M \leq 3 \times 10^5$ * $1 \leq A_i, B_j < 998244353$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $M$ $A_1$ $A_2$ $\cdots$ $A_N$ $B_1$ $B_2$ $\cdots$ $B_M$ [samples]
Samples
Input #1
4 2
1 6 9 2
3 1
Output #1
26

The answer is $|1-3| + |1-1| + |6-3| + |6-1| + |9-3| + |9-1| + |2-3| + |2-1| = 2+0+3+5+6+8+1+1 = 26$.
Input #2
8 8
185991676 311812083 311812083 84357963 185991676 185991676 724020528 369175631
455049197 387671868 4361724 724020528 724020528 455049197 455049197 724020528
Output #2
529117255
API Response (JSON)
{
  "problem": {
    "name": "Sum of Differences",
    "description": {
      "content": "You are given a length-$N$ sequence of positive integers $A = (A_1, A_2, \\dots, A_N)$ and a length-$M$ sequence of positive integers $B = (B_1, B_2, \\dots, B_M)$. Find the value of $\\displaystyle \\sum",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc437_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a length-$N$ sequence of positive integers $A = (A_1, A_2, \\dots, A_N)$ and a length-$M$ sequence of positive integers $B = (B_1, B_2, \\dots, B_M)$.\nFind the value of $\\displaystyle \\sum...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments