(Jakarta) Shai Gilgeous-Alexander scored 30 points, including 14 on 16 free throws, as Canada defeated Spain 88-85 to advance to the quarterfinals at the Basketball World Cup.

This victory also allows Canada to obtain its ticket for the Olympic Games in Paris, next summer.

Canada will play Slovenia on Wednesday in the quarter-finals. The Slovenians are led by Luka Doncic, the World Cup leader with 26.4 points per game, on average.

Dillon Brooks had a strong defensive game and provided 22 points for Canada (4-1), while Gilgeous-Alexander added seven assists and four rebounds.

Luguentz Dort of Montreal had three points, six assists, a steal and a block.

Willy Hernangomez was the most productive for the Spain side (3-2) with 25 points. Santiago Aldama added 20 points.

A 69-65 loss to Brazil on Friday put Canada in a position where it had to beat Spain to continue its adventure in the tournament.

With Canada trailing 78-74, Gilgeous-Alexander hit a shot in stride and drew a foul. He made the free throw to cut the lead to just one point with 1:51 left.

Hernangomez was fouled at the other end of the field and made both of his free throws to make it 80-77.

Brooks tied the game on the next possession with a three-pointer with 1:12 on the clock. He then caused a turnover by forcing Alex Abrines out of bounds.

Gilgeous-Alexander gave Canada the lead with a two-point jump shot with 43.5 seconds left. After a missed Spanish shot, Gilgeous-Alexander made two free throws, giving Canada a four-point cushion.

The cat-and-mouse game continued, but Gilgeous-Alexander remained perfect every time he was sent to the free-throw line, preventing Spain from getting back into the game.

A last long Spanish shot from three points touched the basket ring without going into the goal, when there was no more time on the clock.