-
Notifications
You must be signed in to change notification settings - Fork 1
/
solve.pas
122 lines (101 loc) · 1.97 KB
/
solve.pas
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
program day9;
type
input = array of int64;
function ReadInput: input;
var
i: integer;
val: int64;
data: input;
begin
i := 0;
repeat
begin
val := 0;
Readln(val);
if val > 0 then
begin
Inc(i);
Setlength(data, i);
data[i-1] := val;
end;
end;
until EOF();
ReadInput := data;
end;
function CheckOutlier(data: input; idx: integer): boolean;
var
i, j: integer;
val, cur: int64;
begin
if idx < 25 then
begin
CheckOutlier := false;
exit
end;
val := data[idx];
for i:= idx-25 to idx - 2 do
begin
cur := data[i];
if cur < val then
for j := i+1 to idx-1 do
begin
if data[j] + cur = val then
begin
CheckOutlier := false;
exit;
end
end;
end;
checkoutlier := true;
end;
function FindOutlier(data: input): integer;
var
i: integer;
begin
FindOutlier := 0;
for i:=0 to length(data)-1 do
begin
if CheckOutlier(data, i) then
begin
FindOutlier := i;
break;
end;
end;
end;
function FindOutlierSum(data: input; idx: integer): int64;
var
i, j: integer;
target, sum, min, max, cur: int64;
begin
target := data[idx];
for i:=0 to idx-1 do
begin
sum := 0;
min := data[i];
max := data[i];
for j:=i+1 to idx-1 do
begin
cur := data[j];
sum := sum + cur;
if cur < min then min := cur;
if cur > max then max := cur;
if sum = target then
begin
FindOutlierSum := min + max;
exit
end;
if sum > target then
break;
end;
end;
end;
var
data: input;
outlierIdx: integer;
outlierSum: int64;
begin
data := ReadInput;
outlierIdx := FindOutlier(data);
outlierSum := FindOutlierSum(data, outlierIdx);
writeln(data[outlierIdx], ' ', outlierSum);
end.