RIO DE JANEIRO (AP) Shaking to samba and expressing a sense of longing with uniquely Brazilian words, Olympians and fans said goodbye to the Rio Games with one last big bash that was both revelatory and a sigh of relief.
The closing ceremony Sunday celebrated the 16-day spectacle that was the Rio Games, which combined numerous highlights with ugly and even bizarre episodes that sometimes overshadowed competition. Cariocas - as Rio's residents are known - weren't swayed by the issues that led up to these Olympics, and braved rain and strong winds on the final night to cap their moment in the worldwide spotlight.
While South America's first Olympics are over, safely and with a grandiose finale, many problems remain. Still, Brazil showed Sunday it still definitely knows how to party.
''These were marvelous Olympic Games in the `marvelous city,''' said International Olympic Committee President Thomas Bach, playing off the ''cidade maravilhosa'' nickname of Brazil's postcard city of inviting coastlines, year-round sun and lush tropical vegetation.
While the stadium erupted in applause at that declaration, a few minutes later there were boos of sadness when Bach announced: ''I declare the Games of the XXXI Olympiad closed.''
The closing ceremony in iconic Maracana Stadium was also meant to take care of some business - formally signaling the transition to the 2020 Summer Olympics in Japan.
But Sunday's party was all about Brazil, designed to be more low-key than the opening, which focused heavily on Rio.
The ceremony featured original footage of Alberto Santos Dumont, the man that Brazilians recognize as the inventor of the airplane. The theme, ''Brazilians can do with their bare hands,'' was a nod to the emerging economy of the world's fifth most populous nation.
Dressed in colorful feathers, dozens of dancers formed in the shape of the arches of Lapa, a popular area of Rio akin to Roman ruins, then morphed to make the shape of iconic Sugarloaf before quickly changing again, this time to the official 2016 symbol.
Samba legend Martinho da Vila, whose tunes make their way into many popular telenovelas, sang ''Carinhoso,'' or ''Affectionate.''
Olympians poured in under light rain, waving their flags while many shook their bodies to samba-infused pop that made the stadium feel like a Carnival parade. Britain's athletes wore shoes with soles that lit up in changing colors of red, white and blue, while Tongan taekwondo athlete Pita Taufatofua danced onstage in a grass skirt as a DJ performed, reprising a moment that captured attention when he carried the flag for his country during the opening ceremony.
The show widened its lens to greater Brazil, a massive country with a land mass slightly larger than the continental United States. There was a tribute to cave paintings of some of the first inhabitants of the Americas, in Serra da Capivara, in Northeastern Brazil, today one of the nation's poorest regions.
Spectators watched performers shake it to frevo, a frenetic dance that - if it's even possible - makes high-octane samba seem like a staid ballroom affair. Holding small umbrellas, dancers jumped and marched while performing acrobatics.
They shook it to ''Vassourinhas,'' which means ''small brooms,'' a popular song that was also the name of a famous club in the northeastern city of Recife.
The show also built performances around ''saudade,'' which means anything from longing for someone to sadness to remembering good times. It is one of the most important words in Brazilian Portuguese. Lights flashed translations for the word in many languages, and a group of women sang ''Mulher Rendeira,'' or ''Lace-making Woman,'' a nod to the country's African heritage. Brazil was the last country in the Americas to outlaw slavery, in 1888.
The games had many memorable moments, both for Brazilian competitors at home and athletes from around the world.
Soccer-crazed Brazil got partial payback against Germany, winning gold two years after a 7-1 World Cup semi-final shellacking that left Brazilians fuming. American gymnast Simone Biles asserted her dominance with four golds, swimmer Michael Phelps added five more to up his staggering total to 23 and the world's fastest man, Usain Bolt, put on his usual show with three golds just days before turning 30 years old.
But there were also ugly episodes, like American swimmer Ryan Lochte's fabricated story about a harrowing robbery that was actually an intoxicated-fueled vandalism of a gas station bathroom, and bizarre issues like Olympic diving pools going from crystal blue to gunky, algae green - at a time when Rio's water quality in open waters is one of the biggest local environmental issues.
With the games over, Brazilians now return to problems that have long consumed the country of 200 million people. The economy is mired in its worst recession in decades, and later this week the Senate is expected to begin the trial on whether to permanently remove suspended President Dilma Rousseff, who was impeached in May for breaking fiscal rules in her managing of the federal budget.
There's widespread expectation that the games in Tokyo, one of the world's richest, most recognizable, cosmopolitan cities, will run more smoothly than they have in Rio. But there's also worry in Japan over whether the Olympics will eventually further drag down an economy that has been struggling for decades.
The governor of Tokyo, Yuriko Koike, accepted the flag from International Olympic Committee President Thomas Bach and Rio Mayor Eduardo Paes, signaling the transition.
Many people, from Brazilians to IOC members, will analyze how things went for the Rio Games in the months ahead. But on Sunday, one strong sentiment was relief - that despite some problems, overall the games went well.
That wasn't a given going in. The Zika virus scared away some competitors and tourists, rampant street crime in Rio and recent extremist attacks around the world raised fears about safety and Brazil's political crisis, and the economic angst behind it, threatened to cast a pall over the competitions.
''We are very resilient, we didn't leave anything important unaddressed,'' said Augusta Porto, 36, a translator and Rio resident. ''We can welcome people despite the serious problems that we have faced in the recent past.''
This story has been corrected to reflect that Brazil's loss to Germany in the 2014 World Cup was in a semifinal, not final.
AP Summer Games website: http://summergames.ap.org
Peter Prengaman on Twitter: www.twitter.com/peterprengaman
Mauricio Savarese on Twitter: www.twitter.com/MSavarese