1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void readNext(const string& line, 
		size_t& startL, 
		size_t& startR,
		string& min) {

	char bufferL[5] = "0000";
        char bufferR[5] = "0000";
        size_t posL=line.find(" ",startL);
	size_t posR=line.rfind(" ",startR-1);
	if(posL == string::npos)
		posL=line.size();
        size_t lenL = posL - startL;
        size_t lenR = startR - (posR+1);
	line.copy(bufferL + 4 - lenL, lenL, startL);
	line.copy(bufferR + 4 - lenR, lenR, posR+1);
	string l(bufferL);
	string r(bufferR);
	if(min > l)
		min.assign(l);
	if(min > r)
		min.assign(r);
	startL=posL+1;
	startR=posR;
}

void processLine(string& line, int n, const int& cost, string& min) {
	size_t left=0;
	size_t right=line.size();
	int i = 1;
	int cCost = i * (n -i +1);
	const int half = (n+1)/2;
	while((cCost <= cost) && (i <= half)) {
		readNext(line, left, right, min);
		++i;
		cCost = i * (n - i + 1);
	}
}

void read_n_and_k(istream& input, int &n, int &k)
{
	string line;
	getline(input, line);
	istringstream s(line.c_str());
	s >> n >> k;
}

void compute(istream& input)
{
	int n, k;
	read_n_and_k(input, n, k);
	string line;
	string min="2019";
	int max_num_rows = n > k ? k : n;
	for(int i = 1; i <= max_num_rows; i++)
	{
		getline(input, line);
		processLine(line, i, k, min);
	}

	istringstream s(min.c_str());
	int a;
	s >> a;
	cout <<  a << endl;
}

int main(int argc, char* argv[])
{
	compute(cin);
	return 0;
}