-
Notifications
You must be signed in to change notification settings - Fork 0
/
2746.java
74 lines (66 loc) · 2.08 KB
/
2746.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static String[] split;
static String line;
static int n;
static int[] nums;
static long[] numCnts;
static boolean[] visited;
static long ans;
static long allSum;
public static void main(String[] args) throws IOException {
n = Integer.parseInt(br.readLine());
nums = new int[n];
numCnts = new long[1_000_001];
visited = new boolean[1_000_001];
split = br.readLine().split(" ");
for (int i = 0; i < n; ++i) {
nums[i] = Integer.parseInt(split[i]);
allSum += nums[i];
++numCnts[nums[i]];
}
Arrays.sort(nums);
// n - 1번이 최대
for (int i = 0; i < n - 1; ++i) {
int a = nums[i];
long com = allSum - 2 * nums[n - 1] - a;
if (com >= nums[n - 1]) {
continue;
}
if (com < 0) {
break;
}
int b = (int) com;
if (!visited[a]) {
if (a == b) {
long cnt = numCnts[a];
ans += cnt * (cnt - 1) / 2;
visited[a] = true;
} else if (numCnts[b] != 0) {
visited[a] = true;
visited[b] = true;
ans += numCnts[a] * numCnts[b];
}
}
}
// n - 2번이 최대
for (int i = 0; i < n - 2; ++i) {
long sum = allSum - nums[n - 1] - nums[n - 2] - nums[i];
if (sum == nums[n - 2]) {
++ans;
} else if (sum < nums[n - 2]) {
break;
}
}
// n - 3번이 최대
if (allSum - nums[n - 1] - nums[n - 2] - nums[n - 3] == nums[n - 3]) {
++ans;
}
System.out.println(ans);
}
}