PORTO ALEGRE, Brazil — After being counted out by most, Peru pulled off a stunning Copa América comeback.

Two weeks after a demoralizing 5-0 loss to Brazil that left the team on the verge of a first-round elimination, Peru got past two-time defending champion Chile 3-0 on Wednesday to make it to the continental final for the first time since 1975.

Peru had already stunned title-favourite Uruguay in the quarterfinals after advancing as one of the two best third-place finishers from the three groups, and now it will get a rematch against host Brazil in Sunday's final at the Maracanã Stadium.

"We are a team that knows how to overcome adversities," Peru coach Ricardo Gareca said. "That's something very unique to our team. That loss (against Brazil) would hurt any team, but we were able to recover from it. We deserve a lot of credit for that."

Chile was trying to become the first team to win three straight Copa América titles since Argentina in the 1940s, but now will play the third-place match against the Argentines on Saturday in a repeat of the last two finals.

"We entered the match already thinking about the final and that's how we allowed Peru to surprise us," Chile coach Reinaldo Rueda said. "We didn't show the attitude and aggressiveness from the previous matches."

Chile's loss to longtime rival Peru will add pressure on Rueda, a Colombian who had been criticized since he took over the squad at the beginning of last year. The result will also raise further questions about its so-called golden generation, which failed to qualify for the 2018 World Cup in Russia.

The veterans of Chile's recent triumphs, including Eduardo Vargas, Arturo Vidal and Alexis Sánchez, had subpar performances at the Arena Grêmio in Porto Alegre.

Despite a team without the same quality as Chile, it was Peru that got off to a good start and dominated the match early on.

"When this team wants something, it works hard and gets it," Peru striker Paolo Guerrero said. "We deserved to make it to the final. We were fully focused from the start."

Edinson Flores opened the scoring with a shot from inside the area in the 21st minute and Yoshimar Yotún added to the lead in the 38th with a strike into the open net after Chile goalkeeper Gabriel Arias left the area to try to stop a counterattack.

Guerrero sealed the victory in a breakaway in stoppage time to become the Copa América's all-time leading scorer among active players with 13 goals.

Chile's Eduardo Vargas could have matched Guerrero when his team was awarded a penalty kick in the final seconds, but he had his weak shot from the spot saved by Peru goalkeeper Pedro Gallese.

Gallese had a great performance to stop Chile's attack, which faltered during most of the match despite creating most of the scoring chances.

The Peruvian goalkeeper made an embarrassing mistake in the rout against Brazil, but had already saved penalty kicks by Uruguay's Luis Suárez and Brazil's Gabriel Jesus.

Peru, which last year made its first World Cup appearance in 36 years, was playing in the semifinals for the third time in the last four Copa Américas.

It had lost seven of its last eight competitive matches against Chile, including in the semifinals of the Copa América in 2015.

The rivalry between Chile and Peru dates to the 19th Century. The nations fought in the War of the Pacific from 1879-84, with Chile coming out victorious against a Peru-Bolivia alliance and taking over some key land with access to the Pacific Ocean.

___

More AP soccer: https://apnews.com/apf-Soccer and https://twitter.com/AP_Sports