Home AtCoder. 014 We Used to Sing a Song Together(3)
Post
Cancel

AtCoder. 014 We Used to Sing a Song Together(3)

[Link] https://AtCoder.jp/contests/typical90/tasks/typical90_n


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.*;
import java.io.*;

public class Main{
  static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  static StringTokenizer st;
  public static void main(String[] args) throws IOException {
    StringBuilder sb = new StringBuilder();
    readLine();
    int n = getInt();
    int[] a = new int[n], b = new int[n];
    readLine();
    for(int i = 0; i < n; i++) {
      a[i] = getInt();
    }
    readLine();
    for(int i = 0; i < n; i++) {
      b[i] = getInt();
    }
    Arrays.sort(a);
    Arrays.sort(b);
    long sum = 0;
    for(int i = 0; i < n; i++) sum += Math.abs(a[i] - b[i]);
    System.out.println(sum);
  }

  static int getInt() { return Integer.parseInt(st.nextToken()); }
  static void readLine() throws IOException { st = new StringTokenizer(br.readLine()); }
}
This post is licensed under CC BY 4.0 by the author.