function res = tcapss(X,param);
% STDSS  
%   u = US
%   r = ROW
%
%   max E sum  beta^t lam {log(Cu[t]/Nu[t])+psi*log(1-Hu[t]/Nu[t])}Nu[t] 
%          t     + (1-lam){log(Cr[t]/Nr[t])+psi*log(1-Hr[t]/Nr[t])}Nr[t]
% 
%   s.t.  Cu[t]+Cr[t]+Xu[t]+Xr[t] = Au[t] F(Ku[t],Hu[t])+ Ar[t] F(Kr[t],Hr[t])
%         Kr[t+1] = (1-delta)Kr[t]+Xr[t]
%         Ku[t+1] = (1-delta)Ku[t]+Xu[t]
%         Ku[0], Kr[0] given
%
%
%   Ellen McGrattan, 9-1-07
%   Revised, ERM, 1-17-08

%
% Parameters
%

alpha = param(1);
beta  = param(2);
delta = param(3);
lam   = param(4);
psi   = param(5);
Au    = param(6);
Ar    = param(7);
Nu    = param(8);
Nr    = param(9);

%
% Variables
%

Ku    = X(1);
Kr    = X(2);
Hu    = X(3);
Hr    = X(4);

%
% Intermediate variables and residuals
%

Xu    = delta*Ku;
Xr    = delta*Kr;
Yu    = Au*Ku^alpha*Hu^(1-alpha);
Yr    = Ar*Kr^alpha*Hr^(1-alpha);
Cu    = (Yu+Yr-Xu-Xr)/(1+(1-lam)*Nr/(lam*Nu));
Cr    = (1-lam)*Cu*Nr/(lam*Nu);
res   = [psi*Cu/(Nu-Hu)-(1-alpha)*Yu/Hu;
         psi*Cr/(Nr-Hr)-(1-alpha)*Yr/Hr;
         1-beta*(alpha*Yu/Ku+1-delta);
         1-beta*(alpha*Yr/Kr+1-delta)];
