import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var left: [Int] = []
var right: [Int] = []
for (index, value) in A.enumerated() {
left.append(index - value)
right.append(index + value)
}
left.sort { $0 < $1 }
right.sort { $0 < $1 }
var result = 0
var current = 0
var leftIndex = 0
var rightIndex = 0
for _ in A {
while leftIndex < A.count && left[leftIndex] <= right[rightIndex] {
current += 1
leftIndex += 1
}
current -= 1
result += current
rightIndex += 1
if result > 10000000 {
return -1
}
}
return result
}