---
id: 96
lang: en
translation_of: index.md
source_sha256: 2d72c762c0eb0b274159858b9bb777ccda2f86a703c38f49ff66ee1f5be3480c
title: "P1070: Road Game — Solution"
description: "After a long look at the solutions: one dimension is enough for this problem, but the O(n^3) time complexity is truly staggering."
tags: ["OI","DP","Nasty Problems","Interval DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The unclosed parenthesis in the transition equation max(F[i], ... is kept as in the original."
---

## Idea

After poring over the solutions for a long time, I understood the ideas of the <span class="slang" data-zh="dalao" data-gloss="Pinyin of 大佬 (dàlǎo), “big guy”. A highly skilled person, an expert; used with admiration.">big shots</span>. One dimension is enough for this problem, but the O(n^3) time complexity is truly staggering. Alas, I'm just too <span class="slang" data-zh="菜" data-gloss="cài, “vegetable”. Slang for being bad at something; weak.">weak</span> and could only understand this one, so all I can do is write an n^3 solution.

Transition equation: F[i] = max(F[i], F[i-k] + sum - cost[curt], where i is the current time, k is the number of steps of the robot currently set up, sum is the accumulated coins, and curt is the index of the current robot. When curt = 0, curt has to be set to n (the circular property). The answer is stored in F[m].

## Code

```cpp
// P1070.cpp
#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 2020;
int table[maxn][maxn];
int cost[maxn];
int F[maxn];
int n, m, p;

int main()
{
    cin >> n >> m >> p;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> table[i][j];
    for (int i = 1; i <= n; i++)
        cin >> cost[i];
    fill(F, F + maxn, -(1e9));
    F[0] = 0;
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++)
        {
            int curt = j - 1;
            if (curt == 0)
                curt = n;
            int sum = table[curt][i];
            for (int k = 1; k <= p; k++)
            {
                if (i - k < 0)
                    break;
                F[i] = max(F[i], F[i - k] + sum - cost[curt]);
                curt--;
                if (curt == 0)
                    curt = n;
                sum += table[curt][i - k];
            }
        }
    cout << F[m];
    return 0;
}
```
