Language: C++

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while(cin>>n,n){
        vector<int> p(n);
        for(int i=0;i<n;i++) cin>>p[i];
        const int INF=1e8;
        vector<vector<pair<int,int>>> dp(n+1,vector<pair<int,int>>(100000,make_pair(-INF,INF)));
        dp[0][0]=make_pair(0,0);
        for(int i=0;i<n;i++){
            for(int j=0;j<dp[i].size();j++){
                if(0<p[i]%1000 && p[i]%1000<=500){
                    pair<int,int> x=make_pair(dp[i][j].first+1,dp[i][j].second-p[i]);
                    int to=j+1000-p[i]%1000;
                    if(to<dp[i+1].size()){
                        dp[i+1][to]=max(dp[i+1][to],x);
                    }
                }
                else if(500<p[i]%1000){
                    int to=j+1000-p[i]%1000;
                    if(to<dp[i+1].size()){
                        dp[i+1][to]=max(dp[i+1][to],make_pair(dp[i][j].first,dp[i][j].second-p[i]));
                    }
                    to=j-(p[i]+500)%1000;
                    if(to>=0){
                        dp[i+1][to]=max(dp[i+1][to],make_pair(dp[i][j].first+1,dp[i][j].second-p[i]));
                    }
                }
                dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
            }
        }
        pair<int,int> res=make_pair(-INF,INF);
        for(int i=0;i<dp[n].size();i++){
            res=max(res,dp[n][i]);
        }
        cout<<res.first<<" "<<-res.second<<endl;
    }
    return 0;

}                        dp[i+1][to]=max(dp[i+1][to],make_pair(dp[i][j].first,dp[i][j].second-p[i]));
                    }
                    to=j-(p[i]+500)%1000;
                    if(to>=0){
                        dp[i+1][to]=max(dp[i+1][to],make_pair(dp[i][j].first+1,dp[i][j].second-p[i]));
                    }
                }
                dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
            }
        }
        pair<int,int> res=make_pair(-INF,INF);
        for(int i=0;i<dp[n].size();i++){
            res=max(res,dp[n][i]);
        }
        cout<<res.first<<" "<<-res.second<<endl;
    }
    return 0;

}

Compiler output:
a.cc:42:26: error: 'dp' does not name a type
   42 | }                        dp[i+1][to]=max(dp[i+1][to],make_pair(dp[i][j].first,dp[i][j].second-p[i]));
      |                          ^~
a.cc:43:21: error: expected declaration before '}' token
   43 |                     }
      |                     ^
a.cc:44:21: error: 'to' does not name a type; did you mean 'tm'?
   44 |                     to=j-(p[i]+500)%1000;
      |                     ^~
      |                     tm
a.cc:45:21: error: expected unqualified-id before 'if'
   45 |                     if(to>=0){
      |                     ^~
a.cc:48:17: error: expected declaration before '}' token
   48 |                 }
      |                 ^
a.cc:49:17: error: 'dp' does not name a type
   49 |                 dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
      |                 ^~
a.cc:50:13: error: expected declaration before '}' token
   50 |             }
      |             ^
a.cc:51:9: error: expected declaration before '}' token
   51 |         }
      |         ^
a.cc:52:38: error: 'INF' was not declared in this scope
   52 |         pair<int,int> res=make_pair(-INF,INF);
      |                                      ^~~
a.cc:52:42: error: 'INF' was not declared in this scope
   52 |         pair<int,int> res=make_pair(-INF,INF);
      |                                          ^~~
a.cc:53:9: error: expected unqualified-id before 'for'
   53 |         for(int i=0;i<dp[n].size();i++){
      |         ^~~
a.cc:53:21: error: 'i' does not name a type
   53 |         for(int i=0;i<dp[n].size();i++){
      |                     ^
a.cc:53:36: error: 'i' does not name a type
   53 |         for(int i=0;i<dp[n].size();i++){
      |                                    ^
a.cc:56:9: error: 'cout' does not name a type
   56 |         cout<<res.first<<" "<<-res.second<<endl;
      |         ^~~~
a.cc:57:5: error: expected declaration before '}' token
   57 |     }
      |     ^
a.cc:58:5: error: expected unqualified-id before 'return'
   58 |     return 0;
      |     ^~~~~~
a.cc:60:1: error: expected declaration before '}' token
   60 | }
      | ^