ICPC 2017 - D. Money for Nothing
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
Source-first archive entry
This page is built from the copied files in competitive_programming/icpc/2017/D-money-for-nothing. Edit
competitive_programming/icpc/2017/D-money-for-nothing/solution.tex to update the written solution and
competitive_programming/icpc/2017/D-money-for-nothing/solution.cpp to update the implementation.
The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.
Problem Statement
Copied statement text kept beside the solution archive for direct reference.
Rapid City
event
sponsor
ICPC 2017
Problem D
Money for Nothing
Time limit: 5 seconds
In this problem you will be solving one of the most profound challenges of humans across the world since
the beginning of time – how to make lots of money.
You are a middleman in the widget market. Your job is to buy widgets from widget producer companies
and sell them to widget consumer companies. Each widget consumer company has an open request for one
widget per day, until some end date, and a price at which it is willing to buy the widgets. On the other hand,
each widget producer company has a start date at which it can start delivering widgets and a price at which
it will deliver each widget.
Due to fair competition laws, you can sign a contract with only one producer company and only one con-
sumer company. You will buy widgets from the producer company, one per day, starting on the day it can
start delivering, and ending on the date specified by the consumer company. On each of those days you earn
the difference between the producer’s selling price and the consumer’s buying price.
Your goal is to choose the consumer company and the producer company that will maximize your profits.
Input
The first line of input contains two integers m and n (1 ≤ m, n ≤ 500 000) denoting the number of producer
and consumer companies in the market, respectively. It is followed by m lines, the ith of which contains
two integers pi and di (1 ≤ pi , di ≤ 109 ), the price (in dollars) at which the ith producer sells one widget
and the day on which the first widget will be available from this company. Then follow n lines, the j th of
which contains two integers qj and ej (1 ≤ qj , ej ≤ 109 ), the price (in dollars) at which the j th consumer is
willing to buy widgets and the day immediately after the day on which the last widget has to be delivered to
this company.
Output
Display the maximum total number of dollars you can earn. If there is no way to sign contracts that gives
you any profit, display 0.
Sample Input 1 Sample Output 1
2 2 5
1 3
2 1
3 5
7 2
Rapid City
event
sponsor
ICPC 2017
Sample Input 2 Sample Output 2
1 2 0
10 10
9 11
11 9
Editorial
Rendered from the copied solution.tex file. The original TeX source remains
available below.
Key Observations
Write the structural observations that make the problem tractable.
State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
If the constraints matter, explain exactly which part of the solution they enable.
Algorithm
Describe the data structures and the state maintained by the algorithm.
Explain the processing order and why it is sufficient.
Mention corner cases explicitly if they affect the implementation.
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
State the first key claim.
Proof.
Provide a concise proof.
Lemma 2.
State the next claim if needed.
Proof.
Provide a concise proof.
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
Combine the lemmas and finish the argument.
Complexity Analysis
State the running time and memory usage in terms of the input size.
Implementation Notes
Mention any non-obvious implementation detail that is easy to get wrong.
Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
Code
Exact copied C++ implementation from solution.cpp.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files
Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}
\title{ICPC World Finals 2017\\D. Money for Nothing}
\author{}
\date{}
\begin{document}
\maketitle
\section*{Problem Summary}
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
\section*{Key Observations}
\begin{itemize}[leftmargin=*]
\item Write the structural observations that make the problem tractable.
\item State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
\item If the constraints matter, explain exactly which part of the solution they enable.
\end{itemize}
\section*{Algorithm}
\begin{enumerate}[leftmargin=*]
\item Describe the data structures and the state maintained by the algorithm.
\item Explain the processing order and why it is sufficient.
\item Mention corner cases explicitly if they affect the implementation.
\end{enumerate}
\section*{Correctness Proof}
We prove that the algorithm returns the correct answer.
\paragraph{Lemma 1.}
State the first key claim.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Lemma 2.}
State the next claim if needed.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.
\paragraph{Proof.}
Combine the lemmas and finish the argument.
\section*{Complexity Analysis}
State the running time and memory usage in terms of the input size.
\section*{Implementation Notes}
\begin{itemize}[leftmargin=*]
\item Mention any non-obvious implementation detail that is easy to get wrong.
\item Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
\end{itemize}
\end{document}
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}