Please sign in first.
Submission #54855773
Source Code Expand
class BinaryHeap
def initialize; @heap = []; end
def empty?; @heap.empty?; end
def size; @heap.size; end
def top; @heap[0]; end
def pop; el = @heap.pop; @heap.empty? ? el : replace_top(el); end
def replace_top(el); @heap[0], x = el, @heap[0]; _down!(0); x; end
def push(x); @heap << x; _up!(@heap.size - 1); end
def inspect;"#<PQ: @heap = #{@heap}>"; end
alias_method :to_s, :inspect
alias_method :<<, :push
private
def _higher?(actual, other); actual[0] < other[0]; end
def _up!(i)
x = @heap[i]
while i > 0
up = (i - 1) >> 1
break if _higher?(@heap[up], x)
@heap[i] = @heap[up]
i = up
end
@heap[i] = x
end
def _down!(up)
x = @heap[up]
n = @heap.size
while (j = 2 * up + 1) < n
j += 1 if j + 1 < n && _higher?(@heap[j + 1], @heap[j])
break if _higher?(x, @heap[j])
@heap[up] = @heap[j]
up = j
end
@heap[up] = x
end
end
PQ = BinaryHeap
pq = PQ.new
N = gets.to_i
A = gets.split.map(&:to_i).sort!
D = Array.new(N, 0)
sum = 0
pq.push([A[0], 0])
(1 ... N).each do |i|
pr, j = pq.pop
D[j] += 1
D[i] += 1
pq.push([(2 * D[i] + 1) * A[i], i])
pq.push([(2 * D[j] + 1) * A[j], j])
sum += pr + A[i]
end
puts sum
Submission Info
| Submission Time | |
|---|---|
| Task | F - Tree Degree Optimization |
| User | tinsep19 |
| Language | Ruby (ruby 3.2.2) |
| Score | 550 |
| Code Size | 1234 Byte |
| Status | AC |
| Exec Time | 555 ms |
| Memory | 47764 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 550 / 550 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt |
| All | 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_01.txt | AC | 42 ms | 17216 KiB |
| 00_sample_02.txt | AC | 43 ms | 17156 KiB |
| 00_sample_03.txt | AC | 43 ms | 17160 KiB |
| 01_test_01.txt | AC | 452 ms | 46416 KiB |
| 01_test_02.txt | AC | 449 ms | 47484 KiB |
| 01_test_03.txt | AC | 460 ms | 47580 KiB |
| 01_test_04.txt | AC | 447 ms | 47564 KiB |
| 01_test_05.txt | AC | 436 ms | 47620 KiB |
| 01_test_06.txt | AC | 465 ms | 47036 KiB |
| 01_test_07.txt | AC | 450 ms | 47272 KiB |
| 01_test_08.txt | AC | 413 ms | 46912 KiB |
| 01_test_09.txt | AC | 464 ms | 46940 KiB |
| 01_test_10.txt | AC | 410 ms | 47764 KiB |
| 01_test_11.txt | AC | 420 ms | 46240 KiB |
| 01_test_12.txt | AC | 416 ms | 47428 KiB |
| 01_test_13.txt | AC | 447 ms | 46412 KiB |
| 01_test_14.txt | AC | 401 ms | 45648 KiB |
| 01_test_15.txt | AC | 401 ms | 46056 KiB |
| 01_test_16.txt | AC | 400 ms | 45712 KiB |
| 01_test_17.txt | AC | 405 ms | 46552 KiB |
| 01_test_18.txt | AC | 404 ms | 43952 KiB |
| 01_test_19.txt | AC | 405 ms | 45164 KiB |
| 01_test_20.txt | AC | 404 ms | 46180 KiB |
| 01_test_21.txt | AC | 533 ms | 45880 KiB |
| 01_test_22.txt | AC | 541 ms | 47512 KiB |
| 01_test_23.txt | AC | 43 ms | 17212 KiB |
| 01_test_24.txt | AC | 42 ms | 17144 KiB |
| 01_test_25.txt | AC | 42 ms | 17224 KiB |
| 01_test_26.txt | AC | 555 ms | 46960 KiB |