IT COULD hardly have been closer. As the final votes were counted in the run-off ballot for Peru’s presidency, Pedro Pablo Kuczynski, a liberal economist, seemed to have defeated Keiko Fujimori by just 39,000 out of almost 18m votes, a margin of 0.2%. After months in which Ms Fujimori had led opinion polls, this was a surprising reversal. It shows how deeply divided Peru is about the legacy of Ms Fujimori’s father, Alberto, who ruled it as an autocrat from 1990 to 2000; he is serving long prison terms for corruption and complicity in human-rights abuses.

With such a narrow mandate, Mr Kuczynski’s first task when he takes office on July 28th will be to show that he can govern a country facing an economic slowdown and characterised by frequent social conflicts. It helps that he has few real policy differences with Ms Fujimori.

Fortune smiled on Mr Kuczynski throughout the race, with the force of the Andean sun. He was an unlikely victor. At 77, he is among the oldest presidential-election winners in Latin American history. He has alternated periods as an official in Peru—manager of the Central Bank in the 1960s, mining minister in the 1980s and economy and prime minister in the early 2000s—with long stints abroad: he studied at Oxford and Princeton, and worked at the World Bank and as a businessman and banker in the United States. His hired campaign guru resigned in despair at the candidate’s lack of political instincts.

In February Mr Kuczynski’s support in the opinion polls was 9% and falling. His chance came when the electoral authority disqualified two other candidates on technicalities. In the first ballot in April, he won just 21% of the vote, well behind Ms Fujimori’s 40%. A week before the run-off vote on June 5th Ms Fujimori led by five points.

In the end strong anti-Fujimori sentiment brought Mr Kuczynski his razor-thin victory. Ms Fujimori is an effective grass-roots campaigner. She built on her father’s support among the poor, who remember his crushing of hyperinflation and terrorism, and his opening of schools and health clinics. But the other half of Peru abhors Fujimorismo, seeing it as the legacy of a corrupt dictatorship.

Wind at his back

Two things pushed Mr Kuczynski over the line. One was a last-minute, grudging endorsement by Verónika Mendoza, a defeated left-wing candidate. Second, the damage done by the revelation in May that the United States’ Drug Enforcement Administration was investigating Joaquín Ramírez, the general secretary of Ms Fujimori’s party, Popular Force, was magnified when Ms Fujimori took three days to suspend him. The affair seemed to bolster Mr Kuczynski’s claim that, if his opponent won, Peru risked becoming a “narco-state”.

The two candidates broadly agreed on maintaining the free-market policies that have helped turn Peru into the fastest-growing of Latin America’s larger economies in this century. But that will not make Mr Kuczynski’s task much easier. Popular Force won 73 of the 130 seats in Peru’s congressional election, also in April; the new president’s party has only 18. Ms Mendoza’s party was interested only in stopping Ms Fujimori, not in helping Mr Kuczynski.

With the mining boom of the 2000s fading, annual GDP growth has slowed, to 2.8% during the past two years. That is still healthy by regional standards. And growth might edge up in 2016, thanks to rising copper output from new mines. But labour-intensive industries, such as construction and manufacturing, are in recession.

Mr Kuczynski’s plan to stimulate the economy focuses on tax cuts and public investment, especially in drinking water, sanitation and health care. His economic adviser, Alfredo Thorne, proposes to cut value-added tax from 18% to 15% and grant big companies a tax break on reinvestment. He says this will pay for itself by curbing evasion and encouraging Peru’s vast array of informal businesses to go legal. He wants to promote formal jobs by cutting severance pay, replacing it with unemployment insurance for new hires.

Mr Kuczynski plans to run a fiscal deficit of up to 3% of GDP; because Peru’s public debt is low and its credit rating strong, this can easily be financed, he says. Tax revenue would increase and the budget would be in balance by 2021, he argues. Others think the plan threatens economic stability. “It’s very risky to go for such a big deficit when the results of tax cuts and formalisation are very uncertain,” says Elmer Cuba, Ms Fujimori’s economic adviser. Another goal is to free up some $22 billion in investment in mining and energy projects stalled by local opposition. The new team proposes to raise public investment in the affected areas. It also needs to build on the efforts of Ollanta Humala, the outgoing president, to improve education, health and social protection. The other big issue is rising crime. Mr Kuczynski’s security adviser, Gino Costa, wants to reform the national police, boosting its intelligence branch and integrating its operations more closely with municipal forces. Mr Kuczynski’s prospects turn partly on whether he can strike a deal with Popular Force on issues requiring legislation, such as taxes and reform of municipal water companies. The price of that might be to pardon Mr Fujimori, or at least grant him house arrest. That would be controversial. Carlos Bruce, a congressman in Mr Kuczynski’s party, says the new president might offer Ms Fujimori’s party cabinet posts. For Ms Fujimori, a second narrow defeat will be hard to swallow. In 2011 Mr Humala, a former army officer running on a centre-left platform, beat her by three percentage points. This time she took more care to distance herself from her father, to no avail. Fujimorismo will not vanish, but it might split. Oddly, Ms Fujimori’s brother Kenji, a congressman, failed to vote for her.

Peru is not easy to govern. Mr Humala will leave office isolated and unpopular. Ms Fujimori’s criticism of Mr Kuczynski’s team as elitist technocrats, out of touch with Peruvian life, carried much truth. Unless he finds some skilful political operators, fortune may cease to smile on him.