everything seems okay , where is the problem ? can you explain ? problem link : http://codeforces.com/contest/855/problem/B

my code : https://paste.ubuntu.com/25608466/

# | User | Rating |
---|---|---|

1 | tourist | 3434 |

2 | fateice | 3337 |

3 | Um_nik | 3292 |

4 | OO0OOO00O0OOO0O0…O | 3280 |

5 | Syloviaely | 3274 |

6 | Petr | 3223 |

7 | Swistakk | 3105 |

8 | mnbvmar | 3096 |

9 | yosupo | 3091 |

10 | dotorya | 3081 |

# | User | Contrib. |
---|---|---|

1 | rng_58 | 164 |

2 | tourist | 162 |

3 | csacademy | 153 |

4 | Petr | 150 |

5 | Swistakk | 148 |

6 | Um_nik | 144 |

7 | Nickolas | 142 |

7 | Vovuh | 142 |

9 | BledDest | 138 |

9 | PikMike | 138 |

9 | matthew99 | 138 |

9 | Errichto | 138 |

everything seems okay , where is the problem ? can you explain ? problem link : http://codeforces.com/contest/855/problem/B

my code : https://paste.ubuntu.com/25608466/

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/19/2018 18:20:02 (d2).

Desktop version, switch to mobile version.

User lists

Name |
---|

consider this input: 3 1 1 -1 -1 100 1000

should output: 1000

your solution: -1

But why ? i was always taking the maximum answer greedily....!!!

1 ≤ i ≤ j ≤ k ≤ n.

ooo , may be got it , thank you.

As I told you before, greedy approach will not work here.

You can try Dynamic programming to solve this type of problem where greedy doesn't work. But for this there is a brute force solution.

for each number in the array, let's say that this is

a_{j}and we multiply it withq. So we are fixing the middle element every time. Now we have to finda_{i}anda_{k}[i≤jandj≤k] such thata_{i}*p+a_{k}*ris maximized. So we can easily get anO(n^{2}) solution. How to make itO(n)? Well the array is static. So we can pre-calculate the minimum and maximum in each suffix and prefix of the array and use them instead of running a loop each time to finda_{i}anda_{k}. This is the same solution described in the editorial btw.