While you're doing that, you should maintain the (x,y) coordinate of the bear after each segment. You do that by adding or subtracting a[i] from the bear's x or y coordinate, depending on the value of dir[i]. Finally, you use the distance formula to calculate the bear's distance to the origin. Add the to the total and return the total.
In example 0, the Bear's final (x,y) coordinate is (3,-2), so his distance to the origin is sqrt( (-2)*(-2) + (3*3) ) = 3.6055512755, and his final distance is 10.6055512755.