File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+
3+ inp = sys .stdin .readline
4+
5+ N = int (inp ())
6+ arr = list (map (int , inp ().split ()))
7+ arr .sort ()
8+
9+ min_value = int (1e10 )
10+ answer = [- 1 , - 1 , - 1 ]
11+
12+ # 각 용액을 순회하며
13+ for i in range (N ):
14+ left = i + 1
15+ right = N - 1
16+
17+ # i번 용액을 넣었을 때 값이 가장 0에 가까운 두 값 찾기
18+ while left < right :
19+ if left == i :
20+ left = i + 1
21+ continue
22+ elif right == i :
23+ right = i - 1
24+ continue
25+
26+ total = arr [i ] + arr [left ] + arr [right ]
27+
28+ # 세 용액의 합이 0에 가장 가까우면 업데이트
29+ if abs (total ) < min_value :
30+ min_value = abs (total )
31+ answer = [arr [i ], arr [left ], arr [right ]]
32+
33+ if total == 0 :
34+ break
35+ elif total < 0 :
36+ left += 1
37+ else :
38+ right -= 1
39+
40+ answer .sort ()
41+
42+ for ans in answer :
43+ print (ans , end = " " )
You can’t perform that action at this time.
0 commit comments