結果

問題 No.1288 yuki collection
ユーザー ziitaziita
提出日時 2021-04-14 13:57:13
言語 Rust
(1.77.0 + proconio)
結果
TLE  
実行時間 -
コード長 6,544 bytes
コンパイル時間 23,228 ms
コンパイル使用メモリ 385,612 KB
実行使用メモリ 14,016 KB
最終ジャッジ日時 2024-06-30 13:56:05
合計ジャッジ時間 22,702 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case, unused)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;

// https://atcoder.jp/contests/hokudai-hitachi2019-1/submissions/10518254 より

macro_rules! eprint {
    ($($t:tt)*) => {{
        use ::std::io::Write;
        let _ = write!(::std::io::stderr(), $($t)*);
    }};
}
macro_rules! eprintln {
    () => { eprintln!(""); };
    ($($t:tt)*) => {{
        use ::std::io::Write;
        let _ = writeln!(::std::io::stderr(), $($t)*);
    }};
}
macro_rules! dbg {
    ($v:expr) => {{
        let val = $v;
        eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
        val
    }}
}

macro_rules! mat {
    ($($e:expr),*) => { Vec::from(vec![$($e),*]) };
    ($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
    ($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
    ($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

macro_rules! ok {
    ($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => {
        $a$([$i])*.$f($($t),*)
    };
    ($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { {
        let t = $e;
        ok!($a$([$i])*.$f($($es),*)$(@$t)*@t)
    } };
}

pub fn readln() -> String {
    let mut line = String::new();
    ::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
    line
}

macro_rules! read {
    ($($t:tt),*; $n:expr) => {{
        let stdin = ::std::io::stdin();
        let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
            let line = line.unwrap();
            let mut it = line.split_whitespace();
            _read!(it; $($t),*)
        }).collect::<Vec<_>>();
        ret
    }};
    ($($t:tt),*) => {{
        let line = readln();
        let mut it = line.split_whitespace();
        _read!(it; $($t),*)
    }};
}

macro_rules! _read {
    ($it:ident; [char]) => {
        _read!($it; String).chars().collect::<Vec<_>>()
    };
    ($it:ident; [u8]) => {
        Vec::from(_read!($it; String).into_bytes())
    };
    ($it:ident; usize1) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
    };
    ($it:ident; [usize1]) => {
        $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
    };
    ($it:ident; [$t:ty]) => {
        $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
    };
    ($it:ident; $t:ty) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
    };
    ($it:ident; $($t:tt),+) => {
        ($(_read!($it; $t)),*)
    };
}


pub fn main() {
    let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}

const MOD: usize = 998244353;
// const MOD: usize = 1_000_000_007;
const INF: i64 = std::i64::MAX/100;
// const INF: usize = std::usize::MAX;
// const INF: i128 = std::i128::MAX/100;

#[derive(Copy,Clone)]
struct Edge {
	to: usize,
	cap: i64,
	cost: i64,
	rev: usize,
}

struct MinCostFlow {
	edge: Vec<Vec<Edge>>,
	n: usize,
}

impl MinCostFlow {
	pub fn new(n: usize) -> Self {
		MinCostFlow {
			edge: vec![vec![];n],
			n: n,
		}
	}

	pub fn add_edge(&mut self, s: usize, t: usize, cap: i64, cost: i64) {
		let mut tmp = self.edge[t].len();
		self.edge[s].push(Edge{to: t, cap: cap, cost: cost, rev: tmp});
		let mut tmp = self.edge[s].len();
		self.edge[t].push(Edge{to: s, cap: 0, cost: -cost, rev: tmp-1});
	}

	pub fn add_multi_edge(&mut self, s: usize, t: usize, cap: i64, cost: i64) {
		let mut tmp = self.edge[t].len();
		self.edge[s].push(Edge{to: t, cap: cap, cost: cost, rev: tmp});
		let mut tmp = self.edge[s].len();
		self.edge[t].push(Edge{to: s, cap: cap, cost: -cost, rev: tmp-1});
	}

	pub fn calc(&mut self, s: usize, t: usize, flow: usize) -> i64{
		let mut res = flow;
		let mut cost = 0;
		let mut h = vec![INF;self.n];
		h[s] = 0;
		for x in 0..self.n{
			for i in 0..self.n {
				for j in 0..self.edge[i].len() {
					let e = self.edge[i][j];
					let d = h[i] + e.cost;
					if e.cap > 0 && d < h[e.to] {
						h[e.to] = d;
					}
				}
			}
		}
		for k in 0..flow {
			let mut dist = vec![INF;self.n];
			let mut prevv = vec![self.n;self.n];
			let mut preve = vec![self.n;self.n];
			dist[s] = 0;
			let mut q = BinaryHeap::new();
			q.push(Reverse((dist[s],s)));
			while let Some(Reverse((d,cur))) = q.pop() {
				for j in 0..self.edge[cur].len() {
					let e = self.edge[cur][j];
					let d = dist[cur] + e.cost + h[cur] - h[e.to];
					if e.cap > 0 && d < dist[e.to] {
						dist[e.to] = d;
						prevv[e.to] = cur;
						preve[e.to] = j;
						q.push(Reverse((d,e.to)));
					}
				}
			}
			if dist[t]==INF {
                break;
				// return INF;
			}
			for i in 0..self.n {
				if dist[i]!=INF {
					h[i] += dist[i];
				}
			}
			let mut cur = t;
			loop {
				if cur==s {
					break;
				}
				let e = self.edge[prevv[cur]][preve[cur]];
				self.edge[prevv[cur]][preve[cur]].cap -= 1;
				self.edge[cur][e.rev].cap += 1;
				cost += e.cost;
				cur = prevv[cur];
			}
		}
		cost
	}
}

fn solve() {
    let n = read!(usize);
    let s = read!([char]);
    let a = read!([i64]);
    let mut idx = vec![vec![];4];
    for i in 0..s.len() {
        if s[i]=='y' {
            idx[0].push(i);
        }
        if s[i]=='u' {
            idx[1].push(i);
        }
        if s[i]=='k' {
            idx[2].push(i);
        }
        if s[i]=='i' {
            idx[3].push(i);
        }
    }
    let mut f = MinCostFlow::new(n+2+n);
    let mut off = vec![0;4];
    off[0] = 1;
    for i in 1..4 {
        off[i] = off[i-1] + idx[i-1].len();
    }
    for i in 0..idx[0].len() {
        f.add_edge(0,off[0]+i,1,0);
    }
    for c in 0..3 {
        for i in 0..idx[c].len() {
            f.add_edge(off[c]+i,off[c]+i+n+1,1,0);
            for j in 0..idx[c+1].len() {
                let x = idx[c][i];
                let y = idx[c+1][j];
                if x>y {
                    continue;
                }
                f.add_edge(off[c]+i+n+1,off[c+1]+j,1,-a[x]);
            }
        }
    }
    for i in 0..idx[3].len() {
        f.add_edge(off[3]+i,off[3]+i+n+1,1,0);
        let x = idx[3][i];
        f.add_edge(off[3]+i+n+1,n+1,1,-a[x]);
    }
    let mut ans = -f.calc(0,n+1,idx[0].len());
    println!("{}",ans);
}

fn run() {
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    solve();
}
0