%
% Macros for the contest problems
% for MikTeX: use latex.exe
% License: https://github.com/GassaFM/olymp.sty/blob/master/LICENSE
% Authors: https://github.com/GassaFM/olymp.sty/blob/master/AUTHORS
%

\ProvidesPackage{olymp}

\usepackage{verbatim}


\newif\if@landscape\@landscapefalse
\newif\if@russian\@russianfalse
\newif\if@arabic\@arabicfalse

\DeclareOption{landscape}{
    \@landscapetrue
}
\DeclareOption{russian}{
    \@russiantrue
}
\DeclareOption{arabic}{
    \@arabictrue
}
\ProcessOptions\relax


% -- Setup margins --
%
% Tex defines to large margins for our purposes. 
% So we redefine this to use paper more efficiently
%

\newlength{\thelinewidth}

\if@landscape
    \if@twocolumn
    \else
        \error Landscape is only supported for two column mode
    \fi

    \ifcase\@ptsize % 10 pt
        \hoffset=-15mm
        \voffset=-35mm
        \textheight=165mm
        \textwidth=272mm
    \or % 11 pt
        \hoffset=-13mm
        \voffset=-36mm
        \textheight=166mm
        \textwidth=272mm
    \or % 12 pt
        \hoffset=-12mm
        \voffset=-35mm
        \textheight=162mm
        \textwidth=268mm
    \fi
\else
    \ifcase\@ptsize % 10 pt
        \hoffset=-26.5mm
        \voffset=-35mm
        \textheight=250mm
        \textwidth=175mm
    \or % 11 pt
        \hoffset=-25mm
        \voffset=-37.5mm
        \textheight=255mm
        \textwidth=175mm
    \or % 12 pt
        \hoffset=-20mm
        \voffset=-35mm
        \textheight=245mm
        \textwidth=175mm
    \fi

    \if@twocolumn
        \hoffset=-14.3mm
        \voffset=-38mm
        \textheight=255mm
        \textwidth=188mm
    \fi
\fi

\if@twocolumn
    \thelinewidth=0.47\textwidth
\else
    \thelinewidth=\textwidth
\fi

% -- End of setup margins --

%---------- From package "lastpage" ------------------
\def\lastpage@putlabel{\addtocounter{page}{-1}%
   \immediate\write\@auxout{\string\newlabel{LastPage}{{}{\thepage}}}%
   \addtocounter{page}{1}}
\AtEndDocument{\clearpage\lastpage@putlabel}%
%---------- end of "lastpage" ------------------

% -- Setup sizes --
\newlength{\exmpwidinf}
\newlength{\exmpwidouf}
\newlength{\exmpwidewid}
\newlength{\exmpthreewidinf}
\newlength{\exmpthreewidouf}
\newlength{\exmpthreewidnote}

\newif\ifintentionallyblankpages

\exmpwidinf=0.43\thelinewidth
\exmpwidouf=0.43\thelinewidth
\exmpwidewid=0.9\thelinewidth
\exmpthreewidinf=0.28\thelinewidth
\exmpthreewidouf=0.28\thelinewidth
\exmpthreewidnote=0.30\thelinewidth

\newlength{\afterproblemhead}
\newlength{\afterconstraints}

\renewcommand{\t}[1]{\ifmmode{\mathtt{#1}}\else{\texttt{#1}}\fi}
\if@russian
\renewcommand{\le}{\leqslant}
\renewcommand{\ge}{\geqslant}
\renewcommand{\leq}{\leqslant}
\renewcommand{\geq}{\geqslant}
\else
\parindent=0mm
\parskip=1ex
\fi

% -- End of setup sizes --

% -- Setup keywords --

\if@russian
\def\kw@ProblemTutorial{Разбор задачи}
\def\kw@Problem{Задача}
\def\kw@ProblemAuthor{Автор:}
\def\kw@ProblemDeveloper{Разработчик:}
\def\kw@ProblemOrigin{Источник:}
\def\kw@InputFileName{Имя входного файла:}
\def\kw@OutputFileName{Имя выходного файла:}
\def\kw@TimeLimit{Ограничение по времени:}
\def\kw@MemoryLimit{Ограничение по памяти:}
\def\kw@Feedback{Отображение результатов:}
\def\kw@stdin{стандартный поток ввода}
\def\kw@stdout{стандартный поток вывода}
\def\kw@Specification{Спецификация}
\def\kw@Interaction{Протокол взаимодействия}
\def\kw@Input{Формат входных данных}
\def\kw@Output{Формат выходных данных}
\def\kw@Example{Пример}
\def\kw@Examples{Примеры}
\def\kwExampleNotes{Пояснение}
\def\kw@Explanation{Пояснение к примеру}
\def\kw@Explanations{Пояснения к примерам}
\def\kw@Illustration{Иллюстрация}
\def\kw@Scoring{Система оценки}
\def\kw@Note{Замечание}
\def\kw@Notes{Замечания}
\def\kw@Constraints{Ограничения}
\def\kw@version{версия}
\def\kw@revision{ревизия}
\def\kw@SubtaskOne{Подзадача 1}
\def\kw@SubtaskTwo{Подзадача 2}
\def\kw@SubtaskThree{Подзадача 3}
\def\kw@SubtaskFour{Подзадача 4}
\def\kw@SubtaskFive{Подзадача 5}
\def\kw@SubtaskSix{Подзадача 6}
\def\kw@Subtask{Подзадача}
\def\kw@points{баллы}
\def\kw@Page{Страница}
\def\kw@of{из}
\def\kw@notstated{не указан}
\def\kw@IntentionallyBlankPage{Эта страница специально оставлена пустой}
\def\kw@defaultinputname{тест}
\def\kw@defaultoutputname{ответ}
\else
\def\kw@ProblemTutorial{Problem Tutorial}
\def\kw@Problem{Problem}
\def\kw@ProblemAuthor{Author:}
\def\kw@ProblemDeveloper{Developer:}
\def\kw@ProblemOrigin{Origin:}
\def\kw@InputFileName{Input file:}
\def\kw@OutputFileName{Output file:}
\def\kw@TimeLimit{Time limit:}
\def\kw@MemoryLimit{Memory limit:}
\def\kw@Feedback{Feedback:}
\def\kw@stdin{standard input}
\def\kw@stdout{standard output}
\def\kw@Specification{Specification}
\def\kw@Interaction{Interaction Protocol}
\def\kw@Input{Input}
\def\kw@Output{Output}
\def\kw@Example{Example}
\def\kw@Examples{Examples}
\def\kwExampleNotes{Notes}
\def\kw@Explanation{Explanation}
\def\kw@Explanations{Explanations}
\def\kw@Illustration{Illustration}
\def\kw@Scoring{Scoring}
\def\kw@Note{Note}
\def\kw@Notes{Notes}
\def\kw@Constraints{Constraints}
\def\kw@version{version}
\def\kw@revision{revision}
\def\kw@SubtaskOne{Subtask 1}
\def\kw@SubtaskTwo{Subtask 2}
\def\kw@SubtaskThree{Subtask 3}
\def\kw@SubtaskFour{Subtask 4}
\def\kw@SubtaskFive{Subtask 5}
\def\kw@SubtaskSix{Subtask 6}
\def\kw@Subtask{Subtask}
\def\kw@points{points}
\def\kw@Page{Page}
\def\kw@of{of}
\def\kw@notstated{not stated}
\def\kw@IntentionallyBlankPage{This page is intentionally left blank}
\def\kw@defaultinputname{test}
\def\kw@defaultoutputname{answer}
\fi

\afterproblemhead=3mm
\afterconstraints=2mm

\newcommand{\problemheadfont}{\LARGE}
\newcommand{\problemsectionfont}{\Large}
\newcommand{\problemend}{
\clearpage
\ifintentionallyblankpages
  \ifodd\value{page}
  \else        
     \vspace*{\fill}
     \begin{center}
     \problemheadfont\kw@IntentionallyBlankPage
     \end{center}
     \vspace*{\fill}
     \clearpage
  \fi
\fi       
}
\newcommand{\problemtextfont}{\normalsize}
\newcommand{\beforeproblemsectioncaption}{\smallbreak\smallskip}
\newcommand{\afterproblemsectioncaption}{\smallskip}

\if@twocolumn
    \afterproblemhead=1mm
    \afterconstraints=1mm
    \renewcommand{\problemheadfont}{\large}
    \renewcommand{\problemsectionfont}{\normalsize}
    \renewcommand{\problemend}{\par\medskip}
    \renewcommand{\problemtextfont}{\footnotesize}
    \renewcommand{\beforeproblemsectioncaption}{\smallbreak\smallskip}
    \renewcommand{\afterproblemsectioncaption}{}
\fi

% -- End of setup keywords --


% -- Problem sections --

\newcommand{\createsection}{\@newsection}

\def\@newsection#1#2{\DeclareRobustCommand{#1}{
{\beforeproblemsectioncaption\noindent\bf\problemsectionfont
\textsf{#2}}
\nopagebreak\par\afterproblemsectioncaption}
}

\newcommand{\createsectionpar}{\@newsectionpar}

\def\@newsectionpar#1#2{\DeclareRobustCommand{#1}[1]{
{\beforeproblemsectioncaption\noindent\bf\problemsectionfont
\textsf{#2~##1}}
\nopagebreak\par\afterproblemsectioncaption}
}

\newcommand{\createsectionpartwo}{\@newsectionpartwo}

\def\@newsectionpartwo#1#2#3{\DeclareRobustCommand{#1}[2]{
{\beforeproblemsectioncaption\noindent\problemsectionfont
\textsf{\textbf{#2}~\textbf{##1}~(##2~#3)}}
\nopagebreak\par\afterproblemsectioncaption}
}

\createsection{\Specification}{\kw@Specification}
\createsection{\Interaction}{\kw@Interaction}
\createsection{\InputFile}{\kw@Input}
\createsection{\OutputFile}{\kw@Output}
\createsection{\Example}{\kw@Example}
\createsection{\Examples}{\kw@Examples}
\createsection{\Explanation}{\kw@Explanation}
\createsection{\Explanations}{\kw@Explanations}
\createsection{\Illustration}{\kw@Illustration}
\createsection{\Scoring}{\kw@Scoring}
\createsection{\Note}{\kw@Note}
\createsection{\Notes}{\kw@Notes}
\createsection{\Constraints}{\kw@Constraints}
\createsection{\SubtaskOne}{\kw@SubtaskOne}
\createsection{\SubtaskTwo}{\kw@SubtaskTwo}
\createsection{\SubtaskThree}{\kw@SubtaskThree}
\createsection{\SubtaskFour}{\kw@SubtaskFour}
\createsection{\SubtaskFive}{\kw@SubtaskFive}
\createsection{\SubtaskSix}{\kw@SubtaskSix}
\createsectionpar{\Subtask}{\kw@Subtask}
\createsectionpartwo{\SubtaskWithCost}{\kw@Subtask}{\kw@points}

% -- End of problem sections

% -- Default limits definition --

\if@russian
\def\defaulttimelimit{2 секунды}
\else
\def\defaulttimelimit{2 seconds}
\fi

\if@russian
\def\defaultmemorylimit{256 мебибайт}
\else
\def\defaultmemorylimit{256 mebibytes}
\fi

% -- End of default limits definition --

% -- Problem environment --

\def\defaultproblemauthor{\textit{\kw@notstated}}
\gdef\thisproblemauthor{\defaultproblemauthor}
\def\defaultproblemdeveloper{\textit{\kw@notstated}}
\gdef\thisproblemdeveloper{\defaultproblemdeveloper}
\def\defaultproblemorigin{\textit{\kw@notstated}}
\gdef\thisproblemorigin{\defaultproblemorigin}

\newif\ifdisplayauthor
\newif\ifdisplaydeveloper
\newif\ifdisplayorigin
\newif\ifrevisionsignature
\newif\ifdisplayauthorinfooter
\newif\ifdisplaydeveloperinfooter

\newcounter{problem}
\newcounter{subtasknum}[problem]

\newcommand{\SubtaskWithScore}[1]{%
{\addtocounter{subtasknum}{1}%
\beforeproblemsectioncaption\noindent\problemsectionfont%
\textsf{\textbf{\kw@Subtask~\arabic{subtasknum}}}%
\textsf{~(\kw@points:~#1)}%
\nopagebreak\par\afterproblemsectioncaption}%
}%

\newenvironment{tutorial}[1]{%
    \bigskip%
    \noindent%
    \if@russian%
    \textbf{\problemheadfont\textsf{\kw@ProblemTutorial\ <<#1>>}}%
    \else%
    \textbf{\problemheadfont\textsf{\kw@ProblemTutorial:\ ``#1''}}%
    \fi%
    \nopagebreak%
    \problemtextfont%
}

\newenvironment{@problem}[6]{
\global\let\lastproblemauthor\thisproblemauthor
\global\let\lastproblemdeveloper\thisproblemdeveloper
\global\let\lastproblemorigin\thisproblemorigin

% Hotfix
%\def\@memorylimit{#5}

%% -- Default memory limit --
%% :FIXME:
%\def\@t{#5}
%
%\ifx\@t\empty
%    \def\@memorylimit{\defaultmemorylimit}
%\else
%%\ifcat\par\@t
%%    \def\@memorylimit{\defaultmemorylimit}
%%\else
%    \def\@memorylimit{#5}
%%\fi
%\fi
%% -- End of default memory limit --

% -- No feedback case --
% :FIXME:
\def\@t{#6}

\ifx\@t\empty
    \def\@feedback{}
\else
%\ifcat\par\@t
%    \def\@feedback{}
%\else
    \def\@feedback{\kw@Feedback & #6 \\}
\fi
%\fi
% -- End of no feedback case --

    {
        \noindent
        \refstepcounter{problem}
        \textbf{\problemheadfont\textsf{%
        		\ifdefined\ShortProblemTitle\else{\kw@Problem\ \if@arabic\arabic{problem}\else\Alph{problem}\fi. }\fi%
        		#1%
                \ifdefined\DivisionNumber%
                \if\DivisionNumber2%
                {\ \textit{(Division\ \DivisionNumber)}}%
                \fi%
                \fi%
                }}%
        \nopagebreak%
        \par\vspace{\afterproblemhead}%
        \problemtextfont\parindent=6.5mm%
        \vbox{
            \begin{tabular}{l@{\extracolsep{1cm}}l}
            \ifdisplayauthor%
            \kw@ProblemAuthor & \thisproblemauthor \\
            \fi%
            \ifdisplaydeveloper%
            \kw@ProblemDeveloper & \thisproblemdeveloper \\
            \fi%
            \ifdisplayorigin%
            \kw@ProblemOrigin & \thisproblemorigin \\
            \fi%
            \ifx&#2&%
            \else%
            \kw@InputFileName & \texttt{#2} \\
            \fi%
            \ifx&#3&%
            \else%
            \kw@OutputFileName & \texttt{#3} \\
            \fi%
            \ifx&#4&%
            \else%
            \kw@TimeLimit & #4 \\
            \fi%
            \ifx&#5&%
            \else%
            \kw@MemoryLimit & #5 \\
            \fi%
%            \kw@MemoryLimit & \@memorylimit \\
            \@feedback
            \end{tabular}
        }
        \nopagebreak
        \par\vspace{\afterconstraints}
    }
    \problemtextfont

    \newcommand{\InputFileName}{\ifx&#2&{\kw@defaultinputname}\else{#2}\fi}
    \newcommand{\OutputFileName}{\ifx&#3&{\kw@defaultoutputname}\else{#3}\fi}
}{%
    \global\let\lastproblemauthor\thisproblemauthor%
    \global\let\lastproblemdeveloper\thisproblemdeveloper%
    \global\let\lastproblemdorigin\thisproblemorigin%
    \gdef\thisproblemauthor{\defaultproblemauthor}%
    \gdef\thisproblemdeveloper{\defaultproblemdeveloper}%
    \gdef\thisproblemorigin{\defaultproblemorigin}%
    \problemend%
}

\def\s@tm@cr@s{
    \def\widthin##1{\exmpwidinf=##1\relax}
    \def\widthout##1{\exmpwidouf=##1\relax}
    \def\stretchin##1{\advance\exmpwidinf by ##1\relax}
    \def\stretchout##1{\advance\exmpwidouf by ##1\relax}
    \@ifstar{
        \error Star must not be used in example environment any more
    }
}

% This is magic, which delete space after verbatiminput
\addto@hook{\every@verbatim}{\topsep=0pt\relax}

% :FIXME:
\newenvironment{example}[1][]{
    \s@tm@cr@s#1
    \ttfamily\obeylines\obeyspaces\frenchspacing
    \newcommand{\exmp}[2]{
        \begin{minipage}[t]{\exmpwidinf}\rightskip=0pt plus 1fill\relax##1\medskip\end{minipage}&
        \begin{minipage}[t]{\exmpwidouf}\rightskip=0pt plus 1fill\relax##2\medskip\end{minipage}\\
        \hline
    }

    \newcommand{\exmpfile}[2]{
       \exmp{
          \verbatiminput{##1}
       }{
          \verbatiminput{##2}
       }%
    }


    \begin{tabular}{|l|l|}
        \hline
        \multicolumn{1}{|c|}{\bf\texttt{\InputFileName}}&
        \multicolumn{1}{|c|}{\bf\texttt{\OutputFileName}}\\
        \hline
}{
    \end{tabular}
}

\newenvironment{examplewide}[1][]{%
    \s@tm@cr@s#1
    \ttfamily\obeylines\obeyspaces\frenchspacing
    \newcommand{\exmp}[2]{
        \begin{tabular}{|c|}
        \hline
        \multicolumn{1}{|c|}{\bf\texttt{\InputFileName}}\\
        \hline
        \begin{minipage}[t]{\exmpwidewid}\rightskip=0pt plus 1fill\relax
        ##1
        \medskip\end{minipage}\\
        \hline
        \multicolumn{1}{|c|}{\bf\texttt{\OutputFileName}}\\%
        \hline
        \begin{minipage}[t]{\exmpwidewid}\rightskip=0pt plus 1fill\relax
        ##2    
        \medskip\end{minipage}\\%
        \hline
        \end{tabular}
    }
    \newcommand{\exmpfile}[2]{
       \exmp{
          \verbatiminput{##1}
       }{
          \verbatiminput{##2}
       }%
    }
}{
}

\newenvironment{examplethree}[1][]{
    \s@tm@cr@s#1
    \ttfamily\obeylines\obeyspaces\frenchspacing
    \newcommand{\exmp}[3]{
        \begin{minipage}[t]{\exmpthreewidinf}\rightskip=0pt plus 1fill\relax##1\medskip\end{minipage}&
        \begin{minipage}[t]{\exmpthreewidouf}\rightskip=0pt plus 1fill\relax##2\medskip\end{minipage}&
        \begin{minipage}[t]{\exmpthreewidnote}\rightskip=0pt plus 1fill\relax##3\medskip\end{minipage}\\
        \hline
    }

    \newcommand{\exmpfile}[3]{
       \exmp{
          \verbatiminput{##1}
       }{
          \verbatiminput{##2}
       }{
          ##3
       }%
    }


    \begin{tabular}{|l|l|l|}
        \hline
        \multicolumn{1}{|c|}{\bf\texttt{\InputFileName}}&
        \multicolumn{1}{|c|}{\bf\texttt{\OutputFileName}}&
        \multicolumn{1}{|c|}{\bf\texttt{\expandafter\unexpanded{\expandafter\kwExampleNotes}}}\\
        \hline
}{
    \end{tabular}
}

% -- This is hack to make feedback argument optional

\newenvironment{problem}[5]{%
\def\@ProblemCommon{\begin{@problem}{#1}{#2}{#3}{#4}{#5}}%
\newcommand\@problemSix[1]{\@ProblemCommon{##1}}%
\newcommand\@problemFive{\@ProblemCommon{}}%
\@ifnextchar\bgroup\@problemSix\@problemFive%
}{%
\end{@problem}%
}


% -- End of problem environment --


% -- Declare "shortitems" and "shortnums" environment: it's a "compact itemize" --
\if@twocolumn
    \def\shortitems{\vspace{-1mmplus6mm}\itemize\itemsep-0.618mmplus0.5mm\relax}%
    \def\endshortitems{\vspace{-1mmplus6mm}\enditemize}%
    \def\shortnums{\vspace{-1mmplus6mm}\enumerate\itemsep-0.618mmplus0.5mm\relax}%
    \def\endshortnums{\vspace{-1mmplus6mm}\endenumerate}%
\else
    \def\shortitems{\vspace{-3mmplus2mm}\itemize\itemsep-1.618mmplus0.5mm\relax}%
    \def\endshortitems{\vspace{-3mmplus2mm}\enditemize}%
    \def\shortnums{\vspace{-3mmplus2mm}\enumerate\itemsep-1.618mmplus0.5mm\relax}%
    \def\endshortnums{\vspace{-3mmplus2mm}\endenumerate}%
\fi
% -- end of "shortitems" and "shortnums" declaration --

\newcommand{\thecontestname}{Olympiad in Informatics}
\newcommand{\thecontestlocation}{Somewhere}
\newcommand{\thecontestdate}{Once upon a time}
\newcommand{\therevision}{undefined}

\DeclareRobustCommand{\contestname}{\thecontestname\par\thecontestlocation\unskip, \thecontestdate}

\DeclareRobustCommand{\contest}[3]{
    \renewcommand{\thecontestname}{#1}
    \renewcommand{\thecontestlocation}{#2}
    \renewcommand{\thecontestdate}{#3}
}

\DeclareRobustCommand{\revision}[1]{
    \renewcommand{\therevision}{#1}
}

\makeatletter

\renewcommand{\@oddhead}{
    \parbox{\textwidth}{
        \sffamily
        \begin{center}
            \protect\contestname
            \\[2pt]
            \hrule
        \end{center}
    }
}

\renewcommand{\@oddfoot}{
\gdef\problemletter{\if@arabic\arabic{problem}\else\Alph{problem}\fi}

% Revision signature
\ifrevisionsignature%
%\if@revsign%
	{\gdef\rsigfooter{, \kw@revision\ \therevision}}%
\else%
	{\gdef\rsigfooter{}}%
\fi%

\ifdisplayauthorinfooter%
%\if@newfooter%
	{\gdef\thefooter%
        {~\quad{\kw@Problem\ \problemletter%
        \ifdefined\DivisionNumber{ (Div.~\DivisionNumber)}\fi}%
        {\hfill}%
        {\kw@ProblemAuthor~\lastproblemauthor}%
        {\hfill}%
        {\kw@Page\ \thepage\ \kw@of\ \pageref{LastPage}\rsigfooter}\quad~}}%
\else%
\ifdisplaydeveloperinfooter%
	{\gdef\thefooter%
        {~\quad{\kw@Problem\ \problemletter}%
        {\hfill}%
        {\kw@ProblemDeveloper~\lastproblemdeveloper}%
        {\hfill}%
        {\kw@Page\ \thepage\ \kw@of\ \pageref{LastPage}\rsigfooter}\quad~}}%
\else%
	{\gdef\thefooter%
        {{\hfil}\kw@Page\ \thepage\ \kw@of\ \pageref{LastPage}\rsigfooter\hfil}}%
\fi%
\fi%

    \parbox{\textwidth}{
        \hrule
        \vspace{6pt}
        \sffamily
        \thefooter
    }
}

\makeatother
 
\headheight=2cm
\headsep=6mm

\hfuzz=0.5pt

\sloppy