Problem:
Score : 400 points

Problem Statement

There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i.
You are choosing K among these N pieces to eat.
Your "satisfaction" here will be calculated as follows:

The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".

The base total deliciousness is the sum of the deliciousness of the pieces you eat.

The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.

You want to have as much satisfaction as possible.
Find this maximum satisfaction.

Constraints

1 \leq K \leq N \leq 10^5

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

5 3
1 9
1 7
2 6
2 5
3 1

Sample Output 1

26

If you eat Sushi 1,2 and 3:

The base total deliciousness is 9+7+6=22.

The variety bonus is 2*2=4.

Thus, your satisfaction will be 26, which is optimal.

Sample Input 2

7 4
1 1
2 1
3 1
4 6
4 5
4 5
4 5

Sample Output 2

25

It is optimal to eat Sushi 1,2,3 and 4.

Sample Input 3

6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3

4900000016

Note that the output may not fit into a 32-bit integer type.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused)]

use std::{cell, cmp, collections, fmt, iter, ops};

use cell::{Cell, RefCell};
use cmp::Ordering;
use collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use fmt::{Debug, Display};
use ops::{
    Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Not, Sub, SubAssign,
};

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (src = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        _input_inner!(iter, $($r)*)
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        input!(src = s, $($r)*)
    };
}

#[macro_export]
macro_rules! inputln {
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        _input_inner!(iter, $($r)*)
    };
}

#[macro_export]
macro_rules! _input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = _parse_value!($iter, $t);
        _input_inner!($iter $($r)*)
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = _parse_value!($iter, $t);
        _input_inner!($iter $($r)*)
    };
}

#[macro_export]
macro_rules! _parse_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(_parse_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| _parse_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        _parse_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        _parse_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().expect("No item left").parse::<$t>().expect("Parse error")
    };
}

#[cfg(feature = "debug")]
#[macro_export]
macro_rules! d {
    ($val:expr) => {
        match $val {
            tmp => {
                eprintln!("[{}] {} = {:#?}", line!(), stringify!($val), &tmp);
                tmp
            }
        }
    };
}

#[cfg(not(feature = "debug"))]
#[macro_export]
macro_rules! d {
    ($val:expr) => {
        $val
    };
}

/// ```
/// # use lib::*;
/// assert_eq!(vmin!(0, 1), 0);
/// assert_eq!(vmin!(3, 2, 1), 1);
/// ```
#[macro_export]
macro_rules! vmin {
    ($x:expr, $y:expr) => { std::cmp::min($x, $y) };
    ($x:expr, $($args:expr),*) => { std::cmp::min($x, vmin!($($args),*)) };
}

/// ```
/// # use lib::*;
/// assert_eq!(vmax!(0, 1), 1);
/// assert_eq!(vmax!(3, 2, 1), 3);
/// ```
#[macro_export]
macro_rules! vmax {
    ($x:expr, $y:expr) => { std::cmp::max($x, $y) };
    ($x:expr, $($args:expr),*) => { std::cmp::max($x, vmax!($($args),*)) };
}

pub mod num {
    /// ```
    /// # use lib::*;
    /// assert_eq!(num::gcd(2, 5), 1);
    /// assert_eq!(num::gcd(4, 6), 2);
    /// ```
    pub fn gcd(a: u64, b: u64) -> u64 {
        if b == 0 {
            a
        } else {
            gcd(b, a % b)
        }
    }

    /// ```
    /// # use lib::*;
    /// assert_eq!(num::gcd_slice(&[3]), 3);
    /// assert_eq!(num::gcd_slice(&[2, 5]), 1);
    /// assert_eq!(num::gcd_slice(&[4, 6, 8]), 2);
    /// ```
    pub fn gcd_slice(a: &[u64]) -> u64 {
        a.iter().fold(a[0], |a, b| gcd(a, *b))
    }

    /// ```
    /// # use lib::*;
    /// assert_eq!(num::lcm(2, 5), 10);
    /// assert_eq!(num::lcm(4, 6), 12);
    /// ```
    pub fn lcm(a: u64, b: u64) -> u64 {
        a * b / gcd(a, b)
    }

    /// ```
    /// # use lib::*;
    /// assert_eq!(num::lcm_slice(&[3]), 3);
    /// assert_eq!(num::lcm_slice(&[2, 5]), 10);
    /// assert_eq!(num::lcm_slice(&[4, 6, 8]), 24);
    /// ```
    pub fn lcm_slice(a: &[u64]) -> u64 {
        a.iter().fold(a[0], |a, b| lcm(a, *b))
    }
}

pub mod bits {
    /// ```
    /// # use lib::*;
    /// assert!(!bits::is_high(0b0100, 0));
    /// assert!(bits::is_high(0b0100, 2));
    /// ```
    pub fn is_high(bits: u64, pos: usize) -> bool {
        bits & (1 << pos as u64) != 0
    }

    /// ```
    /// # use lib::*;
    /// assert_eq!(bits::len(0b1010), 4);
    /// assert_eq!(bits::len(0b1), 1);
    /// assert_eq!(bits::len(0b0), 0);
    /// ```
    pub fn len(bits: u64) -> usize {
        for b in 0..64 {
            if is_high(bits, 63 - b) {
                return (64 - b) as usize;
            }
        }

        return 0;
    }
}

#[derive(Clone, Debug)]
pub struct UnionFind {
    parent: Vec<usize>,
    size: Vec<usize>,
    depth: Vec<usize>,
}

/// ```
/// # use lib::*;
/// let mut uf = UnionFind::new(4);
/// assert_eq!(uf.size(0), 1);
/// assert_eq!(uf.find(0), 0);
///
/// uf.union(0, 1);
/// assert!(uf.same(0, 1));
/// assert!(uf.find(0) == 0 || uf.find(0) == 1);
/// assert_eq!(uf.size(0), 2);
///
/// uf.union(1, 2);
/// assert!(uf.same(0, 2));
/// assert!(uf.find(0) == 0 || uf.find(0) == 1); // not 2
/// assert_eq!(uf.size(0), 3);
/// ```
impl UnionFind {
    pub fn new(capacity: usize) -> Self {
        UnionFind {
            parent: (0..capacity).collect(),
            size: vec![1; capacity],
            depth: vec![0; capacity],
        }
    }

    pub fn size(&mut self, a: usize) -> usize {
        let p = self.find(a);
        self.size[p]
    }

    pub fn find(&mut self, a: usize) -> usize {
        if self.parent[a] == a {
            a
        } else {
            let p = self.parent[a];
            self.parent[a] = self.find(p);
            self.parent[a]
        }
    }

    pub fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }

    pub fn union(&mut self, a: usize, b: usize) {
        let a = self.find(a);
        let b = self.find(b);
        if a == b {
            return;
        }

        if self.depth[a] < self.depth[b] {
            self.parent[a] = b;
            self.size[b] += self.size[a];
        } else {
            self.parent[b] = a;
            self.size[a] += self.size[b];

            if self.depth[a] == self.depth[b] {
                self.depth[a] += 1;
            }
        }
    }
}

// std::cmp::Reverse is stable since 1.19
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
struct Rev<T>(T);

impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}

impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        other.0.cmp(&self.0)
    }
}

macro_rules! prefix {
    ($iter: expr) => {
        iter::once(Default::default())
            .chain($iter.into_iter())
            .scan(Default::default(), |acc, x| {
                *acc += x;
                Some(*acc)
            })
    };
}

struct IterSorted<T: Ord> {
    heap: BinaryHeap<T>,
}

trait IntoIteratorSorted {
    type Item;
    type IntoIterSorted: Iterator<Item = Self::Item>;

    fn into_iter_sorted(self) -> Self::IntoIterSorted;
}

impl<T: Ord> IntoIteratorSorted for BinaryHeap<T> {
    type Item = T;
    type IntoIterSorted = IterSorted<T>;

    fn into_iter_sorted(self) -> IterSorted<T> {
        IterSorted { heap: self }
    }
}

impl<T: Ord> iter::Iterator for IterSorted<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.heap.pop()
    }
}

fn main() {
    inputln!(sushi_num: usize, eat_num: usize);

    let mut sushis = vec![BinaryHeap::new(); sushi_num];
    for _ in 0..sushi_num {
        inputln!(t: usize1, d: u64);
        sushis[t].push(d);
    }

    let bests = sushis
        .iter_mut()
        .filter_map(|s| s.pop())
        .collect::<BinaryHeap<_>>();

    // let remainings = sushis
    //     .into_iter()
    //     .map(|s| s.into_iter())
    //     .flatten()
    //     .collect::<BinaryHeap<_>>();
    let mut remainings = BinaryHeap::new();
    for s in sushis {
        for d in s {
            remainings.push(d);
        }
    }

    let bests_prefix = prefix!(bests.into_iter_sorted()).collect::<Vec<u64>>();
    let remainings_prefix = prefix!(remainings.into_iter_sorted()).collect::<Vec<u64>>();

    let ans = (1..bests_prefix.len())
        .filter_map(|k| {
            if eat_num - d!(k) < remainings_prefix.len() {
                Some(d!(bests_prefix[k]) + d!(remainings_prefix[eat_num - k]) + (k * k) as u64)
            } else {
                None
            }
        })
        .max()
        .unwrap();

    println!("{}", ans);
}

#[cfg(test)]
mod tests {
    #[allow(unused)]
    use super::*;

    #[test]
    fn test() {
        //
    }
}