The document proposes a Dynamic Optimal Random Access (DORA) algorithm to optimize vehicle-to-roadside (V2R) communications. It formulates the problem of finding the optimal transmission policy with a single access point as a finite-horizon sequential decision problem and solves it using dynamic programming. It then considers the problem with multiple access points and proposes a joint DORA algorithm. The proposed algorithm achieves the minimal total cost, highest probability of completing file uploads, and highest upload ratio compared to other heuristic schemes.