Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{
- fmt::Debug,
- fs::File,
- io,
- io::Read,
- iter::FromIterator,
- num::ParseIntError,
- str::{FromStr, SplitWhitespace},
- };
- #[inline]
- #[allow(dead_code)]
- fn input() -> String {
- let mut input = String::new();
- unsafe {
- io::stdin().read_line(&mut input).unwrap_unchecked();
- }
- input
- }
- #[inline]
- #[allow(dead_code)]
- fn input_parse<T: FromStr<Err = ParseIntError> + Debug>() -> T {
- let mut input = String::new();
- unsafe {
- io::stdin().read_line(&mut input).unwrap_unchecked();
- input.trim().parse().unwrap_unchecked()
- }
- }
- #[inline]
- #[allow(dead_code)]
- fn input_container<T, C>() -> C
- where
- T: FromStr<Err = ParseIntError> + Debug,
- C: FromIterator<T>,
- {
- let mut input = String::new();
- unsafe {
- io::stdin().read_line(&mut input).unwrap_unchecked();
- }
- input
- .split_whitespace()
- .map(|x| unsafe { x.parse::<T>().unwrap_unchecked() })
- .collect::<C>()
- }
- #[inline]
- #[allow(dead_code)]
- fn input_pair<F, S>() -> (F, S)
- where
- F: FromStr<Err = ParseIntError> + Debug,
- S: FromStr<Err = ParseIntError> + Debug,
- {
- let mut input = String::new();
- unsafe {
- io::stdin().read_line(&mut input).unwrap_unchecked();
- }
- let mut input = input.split_whitespace();
- unsafe {
- (
- input.next().unwrap().parse().unwrap_unchecked(),
- input.next().unwrap().parse().unwrap_unchecked(),
- )
- }
- }
- #[inline]
- #[allow(dead_code)]
- fn input_it(mut input: &mut String) -> SplitWhitespace<'_> {
- unsafe {
- io::stdin().read_line(&mut input).unwrap_unchecked();
- }
- input.split_whitespace()
- }
- trait IterExt: std::iter::ExactSizeIterator {
- #[inline]
- fn filter_indexed(&mut self, mut f: impl FnMut(&Self::Item, usize) -> bool) -> Vec<Self::Item> {
- let mut filter = Vec::with_capacity(self.len());
- for i in 0..self.len() {
- let nxt = unsafe { self.next().unwrap_unchecked() };
- if f(&nxt, i) {
- filter.push(nxt)
- }
- }
- filter.shrink_to_fit();
- filter
- }
- }
- impl<T> IterExt for std::slice::Iter<'_, T> {}
- trait VecExt<T: Ord + Clone> {
- fn sorted_self(self) -> Self;
- fn sorted_cloned(&self) -> Self;
- }
- impl<T: Ord + Clone> VecExt<T> for Vec<T> {
- #[inline]
- fn sorted_self(mut self) -> Self {
- self.sort();
- self
- }
- #[inline]
- fn sorted_cloned(&self) -> Self {
- Vec::from(self.as_slice()).sorted_self()
- }
- }
- fn main() {
- let nums = {
- let mut input = String::new();
- unsafe {
- File::open("kek.txt")
- .unwrap_unchecked()
- .read_to_string(&mut input)
- .unwrap_unchecked();
- }
- input
- }
- .trim()
- .split('\n')
- .skip(1)
- .map(|i| unsafe {
- let mut it = i.split(" ").map(|x| x.parse::<u32>().unwrap_unchecked());
- (it.next().unwrap_unchecked(), it.next().unwrap_unchecked())
- })
- .collect::<Vec<_>>();
- let ans = nums
- .iter()
- .fold(0, |acc, (f, s)| acc + std::cmp::min(*f, *s));
- println!(
- "{}",
- ans + match ans % 5 {
- 0 => 0,
- _ => {
- let osts = {
- let difs = nums
- .into_iter()
- .map(|(f, s)| f.abs_diff(s))
- .collect::<Vec<_>>();
- (1..5)
- .map(|o| {
- difs.iter()
- .filter(|&x| x % 5 == o)
- .map(|&x| x)
- .collect::<Vec<_>>()
- .sorted_self()
- })
- .collect::<Vec<_>>()
- };
- unsafe {
- match ans % 5 {
- /// 4:
- /// 4
- /// 3 + 1
- /// 3 + 3 + 3
- /// 2 + 2
- /// 2 + 1 + 1
- /// 1 + 1 + 1 + 1
- 1 => vec![
- *osts.get_unchecked(3).first().unwrap_unchecked(),
- *osts.get_unchecked(2).first().unwrap_unchecked()
- + *osts.get_unchecked(0).first().unwrap_unchecked(),
- osts.get_unchecked(2).iter().take(3).sum::<u32>(),
- osts.get_unchecked(1).iter().take(2).sum::<u32>(),
- osts.get_unchecked(0).iter().take(2).sum::<u32>()
- + *osts.get_unchecked(1).first().unwrap_unchecked(),
- osts.get_unchecked(0).iter().take(4).sum::<u32>(),
- ],
- /// 3:
- /// 4 + 4
- /// 4 + 2 + 2
- /// 3
- /// 2 + 2 + 2 + 2
- /// 2 + 1
- /// 1 + 1 + 1
- 2 => vec![
- osts.get_unchecked(3).iter().take(2).sum::<u32>(),
- *osts.get_unchecked(3).first().unwrap_unchecked()
- + osts.get_unchecked(1).iter().take(2).sum::<u32>(),
- *osts.get_unchecked(2).first().unwrap_unchecked(),
- osts.get_unchecked(1).iter().take(4).sum::<u32>(),
- *osts.get_unchecked(1).first().unwrap_unchecked()
- + *osts.get_unchecked(0).first().unwrap_unchecked(),
- osts.get_unchecked(0).iter().take(3).sum::<u32>(),
- ],
- /// 2:
- /// 4 + 4 + 4
- /// 4 + 3
- /// 3 + 3 + 1
- /// 2
- /// 1 + 1
- 3 => vec![
- osts.get_unchecked(3).iter().take(3).sum::<u32>(),
- *osts.get_unchecked(3).first().unwrap_unchecked()
- + *osts.get_unchecked(2).first().unwrap_unchecked(),
- osts.get_unchecked(2).iter().take(2).sum::<u32>()
- + *osts.get_unchecked(0).first().unwrap_unchecked(),
- *osts.get_unchecked(1).first().unwrap_unchecked(),
- osts.get_unchecked(0).iter().take(2).sum::<u32>(),
- ],
- /// 1:
- /// 4 + 2
- /// 4 + 4 + 3
- /// 4 + 4 + 4 + 4
- /// 3 + 3
- /// 2 + 2 + 2
- /// 1
- _ => vec![
- *osts.get_unchecked(3).first().unwrap_unchecked()
- + *osts.get_unchecked(1).first().unwrap_unchecked(),
- osts.get_unchecked(3).iter().take(2).sum::<u32>()
- + *osts.get_unchecked(2).first().unwrap_unchecked(),
- osts.get_unchecked(3).iter().take(4).sum::<u32>(),
- osts.get_unchecked(2).iter().take(2).sum::<u32>(),
- osts.get_unchecked(1).iter().take(3).sum::<u32>(),
- *osts.get_unchecked(0).first().unwrap_unchecked(),
- ],
- }
- .into_iter()
- .min()
- .unwrap_unchecked()
- }
- }
- }
- )
- }
Add Comment
Please, Sign In to add comment