// The first contact with a backpack is hard to understand! More exercises are required!
# Include "iostream" # include "cmath" using namespace STD; int total [26] [300010]; int main () {int n, m; while (CIN> N> m) {int * price = new int [M + 1]; int * degree = new int [M + 1]; for (INT I = 0; I <= m; I ++) for (Int J = 0; j <= N; j ++) total [I] [J] = 0; For (INT I = 1; I <= m; I ++) CIN> price [I]> degree [I]; int ans = 0; For (INT I = 1; I <= m; I ++) for (Int J = 1; j <= N; j ++) {If (price [I] <= J) {If (total [I- 1] [J] <total [I-1] [J-price [I] + price [I] * degree [I]) // Dynamic Programming formula! Be thorough and familiar with application! {Total [I] [J] = total [I-1] [J-price [I] + price [I] * degree [I]; ans = max (total [I] [J], ANS);} else {total [I] [J] = total [I-1] [J]; ans = max (total [I] [J], ANS) ;}} else {total [I] [J] = total [I-1] [J]; ans = max (total [I] [J], ANS) ;}} cout <ans <Endl ;}}