(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro nlet (name args &body body)
  (labels ((ensure-list (x) (if (listp x) x (list x))))
    (let ((args (mapcar #'ensure-list args)))
      `(labels ((,name ,(mapcar #'car args) ,@body))
         (,name ,@(mapcar #'cadr args))))))

(declaim (inline split-ints-into-vector))
(defun split-ints-into-vector (string dest-vector &key (offset 0) (key #'identity))
  (declare (string string)
           (function key)
           ((simple-array * (*)) dest-vector)
           ((integer 0 #.most-positive-fixnum) offset))
  (loop for idx from offset below (length dest-vector)
        for pos1 = 0 then (1+ pos2)
        for pos2 = (position #\space string :start pos1 :test #'char=)
        do (setf (aref dest-vector idx)
                 (funcall key (parse-integer string :start pos1 :end pos2)))
        finally (return dest-vector)))

(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (terminate-char #\Space))
  "Note that the returned string will be reused."
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let ((,buffer (load-time-value (make-string ,buffer-size
                                                  :element-type 'base-char))))
       (declare (simple-base-string ,buffer))
       (loop for ,character of-type base-char =
                #-swank (code-char (read-byte ,in nil #.(char-code #\Newline)))
                #+swank (read-char ,in nil #\Newline)
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (setf (schar ,buffer ,idx) ,terminate-char)
                     (return (values ,buffer ,idx))))))

(defmacro split-ints-and-bind (vars string &body body)
  (let ((pos1 (gensym "POS"))
	(pos2 (gensym "POS"))
	(str (gensym "STR")))
    (labels ((expand (vars &optional (init-pos1 t))
	       (if (null vars)
		   body
		   `((let* ((,pos1 ,(if init-pos1 0 `(1+ ,pos2)))
			    (,pos2 (position #\space ,str :start ,pos1 :test #'char=))
			    (,(car vars) (parse-integer ,str :start ,pos1 :end ,pos2)))
		       ,@(expand (cdr vars) nil))))))
      `(let ((,str ,string))
         (declare (string ,str))
	 ,@(expand vars)))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Hauptteil
(defun main ()
  (let* ((n (read))
         (k (read))
         ;; (type . point)
         (ts (make-array n :element-type 'list))
         (mem (make-array n :element-type 'uint32 :initial-element 0)))
    (dotimes (i n)
      (split-ints-and-bind (tt s) (buffered-read-line)
        (setf (aref ts i) (cons (- tt 1) s))))
    (setf ts (stable-sort ts #'> :key #'cdr))
    (let* (basic-list                   ; ascending list
           (basic-score (loop for i below k
                              do (push (aref ts i) basic-list)
                                 (incf (aref mem (car (aref ts i))))
                              sum (cdr (aref ts i))))
           (basic-k (count-if-not #'zerop mem))
           (rest-list (loop for i from k below n
                            when (zerop (aref mem (car (aref ts i))))
                            collect (aref ts i))) ; descending list
           score-log)
      (nlet rec ((current-list basic-list)
                 (current-score basic-score)
                 (current-k basic-k)
                 (rest-list rest-list))
        (push (+ current-score (* current-k current-k)) score-log)
        (when (and (< current-k k) current-list rest-list)
          (if (< 1 (aref mem (car (car current-list))))
              (if (zerop (aref mem (car (car rest-list))))
                  (progn (decf (aref mem (car (car current-list))))
                         (incf (aref mem (car (car rest-list))))
                         (rec (cdr current-list)
                              (+ current-score (- (cdr (car rest-list))
                                                  (cdr (car current-list))))
                              (+ current-k 1)
                              (cdr rest-list)))
                  (rec current-list current-score current-k (cdr rest-list)))
              (rec (cdr current-list) current-score current-k rest-list))))
      (println (reduce #'max score-log)))))

#-swank(main)


Measured memory_bytes: 30568