結果

問題 No.324 落ちてた閉路グラフ
ユーザー Leonardone
提出日時 2015-12-17 01:00:14
言語 Ruby
(3.4.1)
結果
TLE  
実行時間 -
コード長 1,705 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-09-16 06:41:02
合計ジャッジ時間 7,504 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other TLE * 1 -- * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#! ruby
# yukicoder My Practice
# author: Leonardone @ NEETSDKASU

def gs() gets.chomp end
def gi() gets.to_i end
def gss() gets.chomp.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j|pr.call(i,j)}} end


$N, $M = gis
$W = gis

if $M < 2
    puts 0
    exit
end

$minselln = $M * 2 > $N ? $M * 2 - $N : 0
$maxselln = $M - 1

$ans = $M * 2 > $N ? $W.take($M * 2 - $N).inject(:+) : 0

$ar = [true] * $N
$v = 0
$ln = 0
$vt = 0

def f(s)
    if $vt > $M
        return
    end
    if $vt == $M
        $ans = [$ans, $v].max
    end
    (s...$N).each do |i|
        if $ar[i]
            $ar[i] = false
            avt = 2
            c1 = false
            if !$ar[i - 1]
                avt -= 1
            elsif !$ar[i - 2]
                c1 = true
                $ar[i - 1] = false
                $v += $W[i - 1]
            end
            c2 = false
            if !$ar[(i + 1) % $N]
                avt -= 1
            elsif !$ar[(i + 2) % $N]
                c2 = true
                $ar[(i + 1) % $N] = false
                $v += $W[(i + 1) % $N]
            end
            $v += $W[i]
            $vt += avt
            f(i + 1)
            $vt -= avt
            if c1
                $v -= $W[i - 1]
                $ar[i - 1] = true
            end
            if c2
                $v -= $W[(i + 1) % $N]
                $ar[(i + 1) % $N] = true
            end
            $v -= $W[i]
            $ar[i] = true
        end
    end
end

f(0)

puts $ans
0