MICHAEL JACKSON BROUGHT Santa Marta a moment of fame. In February 1996 the King of Pop landed by helicopter at the top of one of Rio de Janeiro’s most notorious favelas. Politicians tried to stop him, but Mr Jackson had permission from the drug barons who ruled the slum. He danced down the steep paths between shacks clinging precariously to the mountainside, surrounded by a cheering crowd of Rio’s poorest citizens, and belted out his hit single “They don’t care about us”. The music video was played around the world. It trained a spotlight on Rio’s poverty and inequality.

Sixteen years later Santa Marta is once again a showcase, but of a better sort. It was the first favela to be “pacified” under a government plan to wrest control of Rio’s slums from the drug lords. The place was stormed by the army in 2008. It now has a police station, and is peaceful. It is a thriving example of the boom at the bottom of Brazilian society.

Meet Salete Martins, a bubbly 42-year-old, whose family moved to Santa Marta from Brazil’s north-east when she was eight. By day she works as a trainee tour guide, showing visitors around her neighbourhood for a city-financed non-profit group called Rio Top Tours. At night she studies tourism at a local college. At weekends she sells Bahian food from a bustling stall near the favela’s entrance. And in between she flogs a popular line of beauty products. Her monthly income is around 2,000 reais ($985), four times as much as she made selling sandwiches three years ago and more than three times the minimum wage. She plans to launch her own tour-guide company before the end of this year.

Ms Martins’s success is striking, even in Santa Marta. But it mirrors a trend that has swept the whole of Latin America. Poor people’s incomes have surged over the past decade, leading to a big drop in inequality. In most Latin American countries the Gini coefficient in 2010 was lower than in 2000. The region’s average, at 0.5, is down from almost 0.54 a decade ago, and lower than at any time in the past 30 years (see chart 3), though still high relative to other regions. Judging by evidence from Argentina, the only country in Latin America to publish statistics on tax returns of top earners, the richest 1% are still pulling ahead of the rest. But that concentration is more than made up for by the narrowing of gaps further down the income scale. Both shifts are reflected in popular culture. “Mulheres ricas” (“Rich women”) is a new reality-TV show about Brazil’s ultra-wealthy (“I bathe in mineral water every day,” said one woman in an early episode). But the country’s most popular prime-time soap is “Avenida Brasil”, which documents life among the newly minted middle classes. Although Latin America saw only half the average GDP growth of emerging Asia over the past ten years, its poverty rate fell by 30%. Around a third of the decline is due to improvements in income distribution. How did a continent that had been egregiously unequal since the conquistadores’ land grab suddenly change course? Not because of radical nationalisation and redistribution. Latin America has a few asset-seizing hard-left governments, notably Argentina and Venezuela, but inequality has also fallen in countries following a more orthodox economic course, such as Chile and Colombia. Nor is the turnaround just a side-effect of the commodities boom. Inequality has fallen in countries that rely heavily on exports of commodities, such as Peru, but also in those where manufacturing plays a bigger role, such as Mexico. Nor can demography be the main cause. Poorer Latin American families have become smaller, which reduces inequality, but these changes were well under way in the 1980s and 1990s. According to Nora Lustig, an economist at the University of Tulane and one of the first to document the narrowing of the region’s income gaps, two things have made a big difference. First, the premium for skilled workers has been falling: a surge in secondary education has increased the supply of literate, reasonably well-schooled workers, and years of steady growth have raised relative demand for the less skilled in the formal workforce, whether as construction workers or cleaners. Second, governments around Latin America have reinforced the narrowing of wage gaps with social spending targeted at people with the lowest incomes. These include more generous pensions and conditional cash transfers—schemes that offer payment to the poorest families in return for meeting specific conditions, such as making sure their children go to school.

The most striking change has been in education. In the past Latin American governments lavished cash on universities. State primary and secondary schools were underfunded and of appalling quality. That bias in favour of tertiary education, perversely, most benefited the children of the rich, who had attended private primary and secondary schools. But since the early 1990s education spending has become much more progressive, with a huge expansion in public secondary education among the poor. According to Karla Breceda, Jamele Rigolini and Jaime Saavedra, three economists at the World Bank, Latin American governments, on average, now spend a larger share of GDP on education for the poorest 20% of children than does the United States.

More progressive spending has produced results. Some countries have seen an increase of 20 percentage points in the share of children finishing secondary school. Another study for the World Institute for Development Economics Research in Helsinki by Guillermo Cruces, Carolina García Domench and Leonardo Gasparini showed that the gap between rich and poor in secondary-school enrolment has fallen in all countries except El Salvador, Honduras, Guatemala and Nicaragua.

Many Latin countries are also championing pre-school education. Rio’s city government, for instance, has dramatically increased its network of nursery schools since 2009, building 74 new ones in the past three years. Any child from a family below the poverty line is guaranteed a free place in a nursery from the age of six months.

A nudge in the right direction

Conditional cash transfers (CCTs) reinforce this focus on schooling. These stipends cost relatively little (typically 0.2-0.8% of GDP) but influence the priorities of many. About a quarter of Brazil’s population now gets some money from Bolsa Família, the country’s CCT scheme. State and local governments piggyback on top. In Rio, for instance, the city supplements Bolsa Família payments for 700,000 of its poorer families. If children do exceptionally well in exams, a bonus is paid. If they miss school, the payment stops. Ms Martins realised her 14-year-old was skipping school only when her monthly stipend was docked. Several academic studies in Mexico show that kids in CCT schemes stay at school longer.

Better education is boosting social mobility. Historically, the link between parents’ and children’s education has been closer in Latin America than anywhere else. In Peru, for instance, almost 70% of a child’s educational achievement can be predicted from its father’s schooling. But a forthcoming report from the World Bank suggests that the current generation of Latin American children are both better educated than their parents and moving relatively faster up the education ladder. And, like India’s poorest castes, disadvantaged indigenous people have made big gains.

These newly educated workers enjoy far better prospects in the formal workforce than their parents did. State pensions have become more generous. Countries from Argentina to Bolivia have introduced non-contributory pension schemes—in effect, a promise of government support for the elderly. Minimum wages across the continent have soared. Brazil’s has risen by more than 50% in real terms since 2003. And since pension benefits are linked to the minimum wage, the two trends reinforce each other.

The precise contribution of better education, better opportunities for less skilled workers and bigger social spending differs by country. An analysis by Ms Lustig, Luis López-Calva of the World Bank and Eduardo Ortiz-Juarez of the United Nations Development Programme suggests that narrower wage gaps explain most of the reduction in inequality throughout the region. According to calculations by Marcelo Neri, of the Institute for Applied Economic Research, government transfers explain about one-third of the drop in inequality in Brazil.

So far, so good. But will these gains last? In education, the big challenge is to complement quantity with quality. Latin America has now reaped the benefits that come from simply getting more children into school for longer. But most of the state schools are still much less good than their private equivalents. Virtually all middle- and upper-class children still go to private primary and secondary schools. Until those gaps in quality have been eliminated, educational inequities will persist. They are behind the recent wave of protests over education in Chile.

The more immediate challenge is how to pay for all this. Latin American states have traditionally not been progressive in outlook. Put crudely, governments raised revenue from the more affluent, then spent it on generous public pensions for those same people. Even now, 60% of transfer spending in Bolivia, for example, goes to people who are not poor. Mr Saavedra calls it a “fragmentary social contract”. Governments fail to provide good public services, and middle-class people rely on private education and health care. But they do get generous pensions in return for their taxes.

The long boom of the 2000s allowed a painless change to this social contract. Sustained growth brought in enough tax revenue to boost both education spending and transfers at the bottom without pushing up tax rates. The boom also allowed huge increases in minimum wages without apparent damage to employment. But as growth slows and the real value of minimum wages rises, that combination is becoming unviable.

If the improvements in inequality are to be maintained, let alone continued, tough choices will have to be made. Middle-class entitlements will need to be squeezed. Much like the United States, many Latin countries will have to decide whether to invest in poorer kids or continue to pay generous pensions to richer old people. In both places the social contract needs to be remade. For evidence that this is possible, turn to Sweden.