-
Notifications
You must be signed in to change notification settings - Fork 0
/
daa.swift
59 lines (51 loc) · 1.4 KB
/
daa.swift
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
class daa {
var first: [Int]
var second: [Int]
var iterfirst: Int = 0
var itersecond: Int = 0
var sum1: Int = 0
var sum2: Int = 0
init(_ first: [Int], _ second: [Int]) {
self.first = first
self.second = second
}
func solve() -> Int {
let length = (first.remove(at: 0), second.remove(at: 0))
while iterfirst < length.0, itersecond < length.1 {
if first[iterfirst] > second[itersecond] {
inc_second()
} else if first[iterfirst] < second[itersecond] {
inc_first()
} else {
inc_first()
inc_second()
if sum1 < sum2 {
sum1 = sum2
} else {
sum2 = sum1
}
}
}
while iterfirst < length.0 {
inc_first()
}
while itersecond < length.1 {
inc_second()
}
return max(sum1, sum2)
}
func inc_first() {
sum1 += first[iterfirst]
iterfirst += 1
}
func inc_second() {
sum2 += second[itersecond]
itersecond += 1
}
}
while true {
let first = readLine()?.split(separator: " ").map { Int(String($0))! }
if first?[0] == 0 { break }
let second = readLine()?.split(separator: " ").map { Int(String($0))! }
print(daa(first!, second!).solve())
}