Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{
- collections::VecDeque,
- 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()
- }
- pub 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> {}
- fn main() {
- let mut input = String::new();
- unsafe {
- File::open("kek.txt")
- .unwrap_unchecked()
- .read_to_string(&mut input)
- .unwrap_unchecked();
- }
- let input = input.split('\n').collect::<Vec<_>>();
- let m = unsafe {
- input
- .get_unchecked(0)
- .split(' ')
- .skip(1)
- .next()
- .unwrap_unchecked()
- .parse::<u32>()
- .unwrap_unchecked()
- };
- let mut products = input
- .iter()
- .skip(1)
- .map(|x| unsafe {
- let mut it = x.split(' ');
- (
- it.next()
- .unwrap_unchecked()
- .parse::<u32>()
- .unwrap_unchecked(),
- it.next().unwrap_unchecked(),
- )
- })
- .collect::<Vec<_>>();
- products.sort();
- let mut a_products = products
- .iter()
- .filter(|(_, x)| *x == "A")
- .collect::<Vec<_>>();
- let mut other_products = products
- .iter()
- .filter(|(_, x)| *x != "A")
- .collect::<VecDeque<_>>();
- let mut sum = 0;
- let max_len = products
- .iter()
- .take_while(|(x, _)| {
- if sum + x > m {
- false
- } else {
- sum += x;
- true
- }
- })
- .count();
- sum = 0;
- let mut cur_a_products = a_products
- .iter()
- .take_while(|(x, _)| {
- if sum + x > m {
- false
- } else {
- sum += x;
- true
- }
- })
- .collect::<Vec<_>>();
- let mut cur_other_products_len = 0;
- loop {
- if sum + unsafe { other_products.front().unwrap_unchecked().0 } > m {
- break;
- }
- cur_other_products_len += 1;
- sum += unsafe { other_products.pop_front().unwrap_unchecked().0 };
- }
- while cur_a_products.len() + cur_other_products_len < max_len {
- sum -= unsafe { cur_a_products.pop().unwrap_unchecked().0 };
- loop {
- if sum + unsafe { other_products.front().unwrap_unchecked().0 } > m {
- break;
- }
- cur_other_products_len += 1;
- sum += unsafe { other_products.pop_front().unwrap_unchecked().0 };
- }
- }
- println!("{} {}", cur_a_products.len(), m - sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement